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

    你可能感兴趣的文章
    PHP开发api接口安全验证
    查看>>
    PHP开发规范PSR
    查看>>
    PHP开发遇到错误0001
    查看>>
    rabbitmq guestguest用户不能远程登录
    查看>>
    php异常处理
    查看>>
    PHP引入了泛型和集合两大重要特性,大大改善 PHP 代码的可维护性和可读性
    查看>>
    PHP引擎php.ini参数优化
    查看>>
    PHP引用(&)使用详解
    查看>>
    php引用及垃圾回收
    查看>>
    php当前时间的集中写法
    查看>>
    php循环比较数组中的值,如何从PHP数组中计算值并在foreach循环中仅显示一次值?...
    查看>>
    php微信 开发笔记,微信WebApp开发总结笔记
    查看>>
    php微信公众号开发access_token获取
    查看>>
    php微信公众号开发微信认证开发者
    查看>>
    php微信公众号开发用户基本信息
    查看>>
    php怎么将对象变成数组,php怎么将对象转换成数组
    查看>>
    RabbitMQ - 消息堆积问题的最佳解决方案?惰性队列
    查看>>
    php怎样比较两数大小,jquery如何判断两个数值的大小
    查看>>
    PHP性能监控 - 开启xhprof(一)
    查看>>
    PHP性能监控 - 怎么看xhprof报告(二)
    查看>>