博客
关于我
牛客网算法——名企高频面试题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/

    你可能感兴趣的文章
    Python 有一个不可变的列表吗?
    查看>>
    Python 机器学习入门之 pandas 的使用
    查看>>
    Python 机器学习实战
    查看>>
    python 杀死子进程_subprocess.popen.kill杀死所有子进程
    查看>>
    Python 条件语句与循环结构详解
    查看>>
    Python 条件运算符解决方法如何工作?
    查看>>
    python 枚举类型
    查看>>
    Python 查询 Mongodb数据库
    查看>>
    python 查询Neo4j多节点的多层关系
    查看>>
    Python多处理导致许多僵尸进程
    查看>>
    Python多处理对象引用
    查看>>
    Python多处理安全地写入文件
    查看>>
    python多处理参数:深拷贝?
    查看>>
    Python多处理使用队列写入同一个文件
    查看>>
    Python多处理.Process:从局部变量开始
    查看>>
    Python多处理-进程数
    查看>>
    python多个定时器_python单线程实现多个定时器示例
    查看>>
    python处理文本_Python - Tokenization
    查看>>
    Python处理数据方向之OpenCV
    查看>>
    python处理一亿条数据_Python数据分析实战(一)
    查看>>