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. centos 中sshd莫名其妙不见了?

    发现问题 遇到问题:首先莫要慌:事出有因:先检查一波: 首先呢,看一下/var/log/yum.log  是否有误删的记录: 如有被误删的操作的话:可以去看看日志:到底咋回事: 然后么 yum ins ...

  2. Java并发工具类(三):控制并发线程数的Semaphore

    作用 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源. 简介 Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数 ...

  3. vue复习(二)

    一.组件介绍 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是挂载点 每个组件模板只能拥有一个根标签 子组件的数据具有作用域,以达到组件的复用 二.局部组件 & ...

  4. ASP.NET5之客户端开发:Grunt和Gulp构建工具在Visual Studio 2015中的高效的应用

    Grunt和Gulp是Javascript世界里的用来做自动压缩.Typescript编译.代码质量lint工具.css预处理器的构建工具,它帮助开发者处理客户端开发中的一些烦操重复性的工作.Grun ...

  5. 彻底搞清楚python字符编码

    在讨论python编码之前,我先了解了几种编码的由来. 一.编码类型 1.ascci码 ascci码由美国人发明,用1个字节(byte)存储英文和字符,前期用了128个,后来新加了其他欧洲国家的符号, ...

  6. Python的进阶:copy与deepcopy区别

    copy()与deepcopy()之间的区分必须要涉及到python对于数据的存储方式. 首先直接上结论: —–我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在.所以 ...

  7. UWP 轨道视图Orbit View

    先看一下效果吧 这是我的Music Tags App里面的效果图,当然你也可以做的比我的更炫. 其实这个效果的实现来自于控件UWP Community Toolkit的OrbitView,所以大家要多 ...

  8. linux 问题收集

    1,错误信息:bunzip2: command not found 解决方法:yum install -y bzip2 2,The X11 forwarding request was rejecte ...

  9. Eclipse的汉化问题

    最近看了很多我周围的同学,也都是刚开始接触Eclipse,但是都头疼于eclipse的汉化问题. 好在的是,Eclipse的汉化比较简单,不用到网上自己下载汉化包,而且关于这个软件的汉化也非常的多,所 ...

  10. 自动化运维工具saltstack04 -- 之jinja模板

    jinjia模板 需求:想让saltstack的file模块分发到minion端的配置文件监听minion端的IP和端口,如何用变量实现?看下面!! 1.jinja模板加grains使apache监听 ...