1. /**
  2. * Source : https://oj.leetcode.com/problems/reorder-list/
  3. *
  4. * Given a singly linked list L: L0→L1→…→Ln-1→Ln,
  5. * reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
  6. *
  7. * You must do this in-place without altering the nodes' values.
  8. *
  9. * For example,
  10. * Given {1,2,3,4}, reorder it to {1,4,2,3}.
  11. */
  12. public class RecordList {
  13. /**
  14. * 将链表的后半部分翻转之后依次插入链表前半部分每个元素后面
  15. *
  16. * 设链表长度为n
  17. * 1. 找到链表后半部分起始位置: n/2+1,使用双指针法,slow每次移动一个,fast每次移动两个,fast移动到最后的时候,slow指向的正好是 n/2+1
  18. * 2. 反转后半部分连链表
  19. * 3. 将反转后的后半部分链表依次插入前半部分,left指向左边,right指向反转后的第一个node,依次插入left的后一个,直到right指向null,
  20. * right每次移动一个node,left每次移动2个node(因为刚刚left后面插入一个节点)
  21. *
  22. * @param head
  23. * @return
  24. */
  25. public LinkedNode record (LinkedNode head) {
  26. if (head == null || head.next == null) {
  27. return head;
  28. }
  29. LinkedNode midNode = findMidNode(head);
  30. LinkedNode reversedList = reverse(midNode);
  31. LinkedNode left = head;
  32. LinkedNode right = reversedList;
  33. while (right != null && right.next != null) {
  34. // 记录将要被插入的元素
  35. LinkedNode target = right;
  36. // 下一个需要被插入的元素
  37. right = right.next;
  38. // 插入target到链表中
  39. target.next = left.next;
  40. left.next = target;
  41. left = left.next.next;
  42. }
  43. return head;
  44. }
  45. private LinkedNode findMidNode (LinkedNode head) {
  46. LinkedNode slow = head;
  47. LinkedNode fast = head;
  48. while (fast != null && fast.next != null) {
  49. slow = slow.next;
  50. fast = fast.next.next;
  51. }
  52. return slow;
  53. }
  54. private LinkedNode reverse (LinkedNode head) {
  55. LinkedNode p = head;
  56. LinkedNode pre = null;
  57. while (p != null) {
  58. LinkedNode next = p.next;
  59. p.next = pre;
  60. pre = p;
  61. p = next;
  62. }
  63. return pre;
  64. }
  65. private class LinkedNode {
  66. int value;
  67. LinkedNode next;
  68. }
  69. /**
  70. * 创建普通的链表
  71. * @param arr
  72. * @return
  73. */
  74. public LinkedNode createList (int[] arr) {
  75. if (arr.length == 0) {
  76. return null;
  77. }
  78. LinkedNode head = new LinkedNode();
  79. head.value = arr[0];
  80. LinkedNode pointer = head;
  81. for (int i = 1; i < arr.length; i++) {
  82. LinkedNode node = new LinkedNode();
  83. node.value = arr[i];
  84. pointer.next = node;
  85. pointer = pointer.next;
  86. }
  87. return head;
  88. }
  89. private static void print (LinkedNode head) {
  90. if (head == null) {
  91. System.out.println("[]");
  92. }
  93. StringBuffer stringBuffer = new StringBuffer("[");
  94. while (head != null) {
  95. stringBuffer.append(head.value);
  96. stringBuffer.append(",");
  97. head = head.next;
  98. }
  99. stringBuffer.deleteCharAt(stringBuffer.length()-1);
  100. stringBuffer.append("]");
  101. System.out.println(stringBuffer);
  102. }
  103. public static void main(String[] args) {
  104. RecordList recordList = new RecordList();
  105. int[] arr = new int[]{1,2,3,4,5,6};
  106. print(recordList.record(recordList.createList(arr)));
  107. }
  108. }

leetcode — reorder-list的更多相关文章

  1. [LeetCode] Reorder List 链表重排序

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  2. [leetcode]Reorder List @ Python

    原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...

  3. Leetcode: Reorder List && Summary: Reverse a LinkedList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  4. [Leetcode] Reorder list 重排链表

    Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...

  5. [LeetCode] Reorder List 反向插入链表

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  6. LeetCode Reorder List

    struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution ...

  7. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  10. 【Leetcode】Reorder List JAVA

    一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...

随机推荐

  1. gulp入门详细教程

    简介:gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以很愉快的编写代码, ...

  2. 简陋的斗地主,js实现

    最近闲了两天没事做,用js写了个斗地主,练习练习.代码和功能都很简陋,还有bug,咋只是聊聊自己的思路. 这里说说斗地主主要包含的功能:洗牌,发牌,玩家出牌.电脑出牌,出牌规则的验证,输赢啥的没有判断 ...

  3. WPF 外发光效果

    WPF的滤镜效果,目前框架自带的只有BlurEffect和DropShadowEffect两种.DropShadowEffect为投影效果,只能显示黑灰颜色的效果,如果想让一个边框达到别的颜色的滤镜效 ...

  4. ssm整合(基于xml配置方式)

    本文是基于xml配置的方式来整合SpringMVC.Spring和Mybatis(基于注解的方式会再写一篇文章),步骤如下: (1)首先自然是依赖包的配置文件 pom.xml <project ...

  5. ettercap的中间人欺骗

    环境准备:kali系统 因为kali系统自带ettercap,比较方便, 不需要安装 ifcofing命令查看当前网关 ,当前的IP是: 172.16.42.1 查找局域网所有主机 通过netdisc ...

  6. 深入浅出了解OCR识别票据原理

    欢迎大家前往云加社区,获取更多腾讯海量技术实践干货哦~ 译者:Mr.Geek 本文翻译自dzone 中Ivan Ozhiganov所发文章Deep Dive Into OCR for Receipt ...

  7. linux_Mysql导入数据基本操作

    创建数据库:Databases 数据库名字;导入数据:    mysql -uroot -proot use   数据库名字 source < sql文件名.sql

  8. Python 项目实践一(外星人入侵)第一篇

    python断断续续的学了一段实践,基础课程终于看完了,现在跟着做三个小项目,第一个是外星人入侵的小游戏: 一 Pygame pygame 是一组功能强大而有趣的模块,可用于管理图形,动画乃至声音,让 ...

  9. TP3.2.3 接入银联支付

    TP3.2.3 接入银联支付 项目接入银联支付的过程, 在此记录下,希望能帮助开发盆友平坑. 银联SKD链接:https://open.unionpay.com/ajweb/product/newPr ...

  10. 自学Zabbix3.8.2-可视化Visualisation-maps网络地图

    自学Zabbix3.8.2-可视化Visualisation-maps网络地图 可以简单的理解为动态网络拓扑图,可以针对业务来配置zabbix map,通过map可以了解应用的整体状况:服务器是否异常 ...