Question:

Given a singly linked list L: L0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

Tips:
给定一个单链表,将链表重新排序,注意不能改变结点的值。
排序规则如下:
L0L1→…→Ln-1Ln,
L0LnL1Ln-1L2Ln-2→…
思路:
重新排序后的链表,前1/2 结点相对顺序不变,而后半部分是逆序。所以我的思路是先将后半部分结点翻转,变为逆序,再将后半部分结点依次插入到前半部分中去。
大致分为三部分:
(1)找到链表的中间位置,将链表分为两部分。
(2)将第二部分链表逆序
(3)将第二部分所有节点依次插入到前半部分结点之间。
代码:
public void reorderList(ListNode head) {if (head == null || head.next == null)return;// Find the part2;第二部分是从slow.next开始的ListNode slow = head;ListNode fast = head;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}System.out.println("slow"+slow.val);ListNode mid = slow.next;slow.next = null;System.out.println("mid"+mid.val);// 将第二部分翻转;ListNode pre = null;ListNode cur = mid;while (cur != null) {if (cur.next != null) {ListNode next = cur.next;System.out.println("next"+next.val);cur.next = pre;pre = cur;cur = next;} else {cur.next = pre;pre = cur;cur=null;}}System.out.println("pre"+pre.val);// append one by one;ListNode p1 = head;ListNode p2 = pre;while (p2 != null) {ListNode n1 = p1.next;ListNode n2 = p2.next;p1.next = p2;p2.next = n1;p1 = p1.next.next;p1 = n1;p2 = n2;}    //printwhile (head != null) {System.out.println(head.val);head = head.next;}}

代码中的一些输出 是为了验证结果的正确性 提交时可删除。leetcode提交版版代码如下:

public void reorderList(ListNode head) {if (head == null || head.next == null)return;// Find the part2;第二部分是从slow.next开始的ListNode slow = head;ListNode fast = head;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}ListNode mid = slow.next;slow.next = null;// 将第二部分翻转;ListNode pre = null;ListNode cur = mid;while (cur != null) {if (cur.next != null) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;} else {cur.next = pre;pre = cur;cur=null;}}// append one by one;ListNode p1 = head;ListNode p2 = pre;while (p2 != null) {ListNode n1 = p1.next;ListNode n2 = p2.next;p1.next = p2;p2.next = n1;p1 = p1.next.next;p1 = n1;p2 = n2;}}

转载于:https://www.cnblogs.com/yumiaomiao/p/8479734.html

【Leetcode】143. Reorder List相关推荐

  1. 【leetcode】937. Reorder Log Files

    题目如下: You have an array of logs.  Each log is a space delimited string of words. For each log, the f ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  4. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  5. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  6. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  7. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  8. 【Leetcode】103. 二叉树的锯齿形层次遍历

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  9. 【Leetcode】79.单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...

最新文章

  1. Android10.0 BroadcastCast广播机制原理
  2. 如何用手机维护Mysql数据库
  3. 【PAT乙级】1056 组合数的和 (15 分)
  4. 神策智能推荐 | 运营后台,你的智能分发“万能助手”
  5. git push代码出现push rejected错误
  6. Javascript 常用功能收集-blogjava
  7. arcgis 分区 属性值_如何使用ArcGIS计算分区河流(管线)总长度
  8. 带码农《手写Mybatis》进度3:实现映射器的注册和使用
  9. 推荐系统中的Embedding
  10. IntelliJ Idea学习笔记004---IDEA中maven没有了
  11. networkx怎么显示图_如何将标签添加到networkx图形中的节点?
  12. springboot整合dubbo注解方式(三)
  13. share 接口的使用
  14. 推荐十五款APP原型设计工具
  15. python求三重积分_三重积分的Python数值计算
  16. 《剪花布条》:从花布条中尽可能剪出几块小饰条
  17. netkeeper不能建立远程计算机连接,Netkeeper客户端常见问题集锦.doc
  18. Install SysBench support MySQL and PostgreSQL
  19. R语言量化:使用WindR下载Wind数据
  20. 难得五年来第一次暑假没有出海,即使最终没有逃过8月份的CPT外业

热门文章

  1. install openni2 on ubuntu
  2. AI学习笔记(十一)CNN之图像识别(下)
  3. 定时任务管理系统 java_几种任务调度的 Java 实现方法与比较(定时任务)(转)...
  4. python集合的元素可以是_Python - 集合与元素之集合定义和基本操作方法
  5. 深度学习自动调参工具,NNI使用
  6. android 梅花布局,Android相对布局实现各种梅花效果
  7. android 屏幕方向改变 重新测量,android – 扫描时自动更改屏幕方向(使用ZXING库)...
  8. 64如何传入后台_如何保证API接口数据安全?
  9. s8050三极管经典电路_电子电路系统的基本概念有哪些?
  10. 概率论符号_考研概率论知识点总结1