【Leetcode】143. Reorder List
Question:
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 this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.
L0→Ln→L1→Ln-1→L2→Ln-2→…
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的更多相关文章
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】937. Reorder Log Files 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...
- 【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 ...
- 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 【转载】MSXML应用总结 概念篇
原文:http://blog.sina.com.cn/s/blog_48f93b530100e9tr.html 微软提供了大量的XML开发工具和技术,而SMXML(Microsoft XML Core ...
- 图解SSH原理
1. 初见SSH SSH是一种协议标准,其目的是实现安全远程登录以及其它安全网络服务. SSH仅仅是一协议标准,其具体的实现有很多,既有开源实现的OpenSSH,也有商业实现方案.使用范围最广泛的当然 ...
- 使用Sublime Text 3作为React Native的开发IDE
1.下载安装Sublime 3 Sublime 3的下载地址:http://www.sublimetext.com/3 选相应的平台进行下载,安装. 2.安装Package Control 默认的Su ...
- Excel表格生成sql语句
假如excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age ,在你的excel表格中增加一列,利用excel的公式自动生成sql语句,方法如下: ...
- java随记
jdk1.8中新增了 LocalDate 与 LocalDateTime等类来解决日期处理方法,同时引入了一个新的类DateTimeFormatter来解决日期格式化问题. LocalDateTime ...
- 树莓派3b添加python时间同步脚本
树莓派没有电池,因此断电后系统时间会停止,直到你开机后又继续计时,所以会造成系统时间和实际时间有很大的误差. 因为项目需要用到本地时间,精度要求不高不想折腾(如果需要高精度,需要安装ntp),所以考虑 ...
- tomcat7时遇到启动报错问题 SEVERE: ContainerBase.addChild: start:
个人说明:错误日志确实是我的,方法都是从其他地儿借鉴过来的,但肯定是能解决错误的方法,亲身体验,安全保障!!! 还有遇到这种问题,多启动几遍可能就行了. 错误日志: SEVERE: Container ...
- python解释 yield 和 Generators(生成器)
yield 和 Generators(生成器) 转自:http://www.oschina.net/translate/improve-your-python-yield-and-generators ...
- 关于linux-centos7 安装完成git后npm突然无法使用问题处理
报错: 解决方法: 查看一下nodejs是否安装,如果没有安装的话安装完成就能解决了
- GlusterFS分布式存储集群-2. 使用
参考文档: Quick Start Guide:http://gluster.readthedocs.io/en/latest/Quick-Start-Guide/Quickstart/ Instal ...