博客
关于我
牛客网算法——名企高频面试题143题(13)
阅读量:400 次
发布时间:2019-03-04

本文共 2610 字,大约阅读时间需要 8 分钟。

????

?????????????????????????????????????????????????????????????

???????

??

?????????????????????????????

  • ????????pre?curr??????????????????
  • ??????????????????
  • ?????????????????pre???????
  • ??pre?curr??????????????????
  • ???????pre????????????
  • ???????????O(n)???????O(1)???????????????

    ????

    public class ????II {    public class ListNode {        int val;        ListNode next;        public ListNode(int val) {            this.val = val;        }    }    public ListNode rever(ListNode head) {        if (head == null) {            return null;        }        ListNode pre = null;        ListNode curr = head;        while (curr != null) {            ListNode future = curr.next;            curr.next = pre;            pre = curr;            curr = future;        }        return pre;    }        public static void main(String[] args) {        // ????        ListNode s1 = new ListNode(1);        ListNode s2 = new ListNode(2);        ListNode s3 = new ListNode(3);        ListNode s4 = new ListNode(4);        s1.next = s2;        s2.next = s3;        s3.next = s4;        ListNode res = rever(s1);        while (res != null) {            System.out.println(res.val);            res = res.next;        }    }}

    ??????????

    ????????????????????????????????????????????

  • ????dummy??????????????
  • ???????????dummy?????????
  • ????????pre?curr?????dummy?????????
  • ????????????????????
  • ?????????dummy????????????????????
  • ????????O(n)???????O(1)???????

    ????

    public class ????II {    public class ListNode {        int val;        ListNode next;        public ListNode(int val) {            this.val = val;        }    }    public ListNode rever1(ListNode head) {        if (head == null) {            return null;        }        ListNode dumy = new ListNode(0);        dumy.next = head;        ListNode pre = dumy;        ListNode curr = head;        while (curr.next != null) {            ListNode future = curr.next;            curr.next = future.next;            future.next = dumy.next;            pre.next = future;        }        return dumy.next;    }        public static void main(String[] args) {        // ????        ListNode s1 = new ListNode(1);        ListNode s2 = new ListNode(2);        ListNode s3 = new ListNode(3);        ListNode s4 = new ListNode(4);        s1.next = s2;        s2.next = s3;        s3.next = s4;        ListNode res = rever1(s1);        while (res != null) {            System.out.println(res.val);            res = res.next;        }    }}

    ????

    ???????1?2?3?4???????4?3?2?1?????????????????

    4321

    ??

    ???????????????????????????????????????????????????????????????????????????????

    转载地址:http://wqch.baihongyu.com/

    你可能感兴趣的文章
    ObjC的复制操作
    查看>>
    Object c将一个double值转换为时间格式
    查看>>
    object detection之Win10配置
    查看>>
    object detection训练自己数据
    查看>>
    object detection错误Message type "object_detection.protos.SsdFeatureExtractor" has no field named "bat
    查看>>
    object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
    查看>>
    object detection错误之no module named nets
    查看>>
    Object of type 'ndarray' is not JSON serializable
    查看>>
    Object Oriented Programming in JavaScript
    查看>>
    object references an unsaved transient instance - save the transient instance before flushing
    查看>>
    Object 类的常见方法有哪些?
    查看>>
    Object-c动态特性
    查看>>
    Object.assign用法
    查看>>
    Object.create
    查看>>
    Object.defineProperty详解
    查看>>
    Object.keys()的详解和用法
    查看>>
    objectForKey与valueForKey在NSDictionary中的差异
    查看>>
    Objective - C 小谈:消息机制的原理与使用
    查看>>
    OBJECTIVE C (XCODE) 绘图功能简介(转载)
    查看>>
    Objective-C ---JSON 解析 和 KVC
    查看>>