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. 20155226 2016-2017-2 《Java程序设计》第9周学习总结

    20155226 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商 ...

  2. 北京Uber优步司机奖励政策(4月25日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. python基础学习1-随机验证码

    import random i=random.randrange(65,90) #根据设置的范围生成随机数字 print(i) c=chr(i)#根据随机产生的数字 然后用chr生成对应ASCII 数 ...

  4. PDO访问方式操作数据库

    mysqli是专门访问MySQL数据库的,不能访问其它数据库.PDO可以访问多种的数据库,它把操作类合并在一起,做成一个数据访问抽象层,这个抽象层就是PDO,根据类操作对应的数据库.mysqli是一个 ...

  5. 【HNOI2007】紧急疏散

    题面 题解 \(\text{HNOI2007}\)真的恐怖 这是集合了所罗门的咒语,胜负一子等神仙题和码农题的一年 所以这道题非常码 二分答案,将门拆点,于是就变成了一个二分图匹配的题目 反正很恶心 ...

  6. codechef FEB19 Manhattan Rectangle

    Manhattan Rectangle 链接 题意: 交互题,询问小于7次,确定一个矩形的位置,每次询问一个点到矩形的曼哈顿距离. 分析: 询问三个顶点,然后解一下方程,求出一个边界,就好办了. 用s ...

  7. CF 348 D. Turtles

    D. Turtles 链接 题意: 给定一个N*M的棋盘,有些格子不能走,问有多少种从(1,1)到(N,M)的两条不相交路径. 分析: lGV定理. 定理:点集A={a1,a2,…an}A={a1,a ...

  8. [VB.NET][C#]调用API获取或设置键盘按键状态

    前言 通过 C# 或 VB.NET,你只需编写少量的代码即可实现一个按键精灵. 第一节 接口 调用系统 API 实现获取或设置指定的按键状态. 获取按键状态 调用 GetAsyncKeyState() ...

  9. 开箱即用 - log4net 日志

    废话少说,先上代码 log4net Demo 好的系统都有日志,log4net 是我在.net平台下用过最爽的日志库,简单易用.功能强大. 基于配置(配置很简单,一看就明,通用,拷去即用): 可同时保 ...

  10. Linux速成(一)

    全部转载自http://www.runoob.com/linux/linux-intro.html 一.Linux 简介 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和 ...