博客
关于我
牛客网算法——名企高频面试题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 For循环多次返回
    查看>>
    python frame_python3 selenium自动化 frame表单嵌套的切换方法
    查看>>
    Python ftplib - 指定端口
    查看>>
    Python furl库:一键搞定复杂URL操作
    查看>>
    Python GC 也会关闭文件吗?
    查看>>
    python gen_key.py 报错 提示找不到OpenSSL lib
    查看>>
    python getattrribute_Python学习——面向对象高级之反射
    查看>>
    Python进阶语法:字典推导式
    查看>>
    python gil_Python中GIL的使用详解
    查看>>
    python glob.glob使用
    查看>>
    python glob的安装和使用
    查看>>
    Python google drive API 下载,文件在哪里?
    查看>>
    Python GPS 模块:读取最新的 GPS 数据
    查看>>
    python grpc入门
    查看>>
    python gRPC测试helloworld
    查看>>
    python gRPC简单示例
    查看>>
    Python GUI 开发:全面指南
    查看>>
    Python GUI编程
    查看>>
    Python进阶语法:列表推导式
    查看>>
    Python hashlib模块
    查看>>