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;
}
//print
while (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;
}
}

【Leetcode】143. Reorder List的更多相关文章

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

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

  2. 【LeetCode】937. Reorder Log Files 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...

  3. 【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 ...

  4. 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...

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

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

  6. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. PHP 注释规范

    注释在写代码的过程中非常重要,好的注释能让你的代码读起来更轻松,在写代码的时候一定要注意注释的规范. php里面常见的几种注释方式: 1.文件头的注释,介绍文件名,功能以及作者版本号等信息 /** * ...

  2. 3-[Mysql]-库操作

    1.系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等 performance_schema: My ...

  3. [BZOJ1974][SDOI2010]代码拍卖会[插板法]

    题意 询问有多少个数位为 \(n\) 的形如 \(11223333444589\) 的数位值不下降的数字在\(\mod p\) 的意义下同余 \(0\). $n\leq 10^{18} ,p\leq ...

  4. ant property file刷新不及时

    一.问题 ant脚本定义file的property,有时往里面写了新的值,去访问时还是旧的值 二.原因分析 应该是已定义的file property,后续更新其值的时候,ant的内存缓存没有及时更新, ...

  5. Object C学习笔记11-数组

    在Object C也提供了类似C#中的Array数组对象,在Object C中使用NSArray 来创建数组:但是在Object C中NSArray 只能存放对象类型的指针,不能存放int,char, ...

  6. 【前端模板之路】一、重构的兄弟说:我才不想看你的代码!把HTML给我交出来!

    写在前面 随着前端领域的发展和社会化分工的需要,继前端攻城湿之后,又一重要岗位横空出世——重构攻城湿!所谓的重构攻城湿,他们的一大特点之一,就是精通CSS配置文件的编写...前端攻城湿跟重构攻城湿是一 ...

  7. 用Micro:bit控制遥控车

    很多遥控车是用Arduino来控制,同样也可以用Micro:bit来控制.这篇文章我们就来做个测试. 这次需要用到扩展板,管脚比较多,请参考下图 一.材料: •micro:bit 二片 •micro: ...

  8. Unity3D之AR开发(一)

    近期研究了下AR技术,下面给大家分享一下. 第一种方法:高通AR(Vuforia) Vuforia插件下载地址(官网): https://developer.vuforia.com/downloads ...

  9. 安装VMware-tools时,一直停在“The path "" is not valid path to the gcc binary.”

    解决方案: 1.先停止安装(ctrl+Z) 2.在终端输入: yum -y update yum -y install kernel-headers kernel-devel gcc 3.重新安装VM ...

  10. 用shell实现bat批处理的pause命令-追加改进

    我参考了这个文章:用shell实现bat的pause http://linux-wiki.cn/wiki/zh-hans/%E7%94%A8shell%E5%AE%9E%E7%8E%B0bat%E7% ...