【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;
}
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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 【转载】COM 组件设计与应用(八)——实现多接口
原文:http://vckbase.com/index.php/wv/1219.html 一.前言 从第五回开始到第七回,咱们用 ATL 写了一个简单的 COM 组件,之所以说简单,是因为在组件中,只 ...
- OpenStack入门篇(二十二)之实现阿里云VPC的SDN网络
1.修改/etc/neutron/neutron.conf配置 [root@linux-node1 ~]# vim /etc/neutron/neutron.conf [defalut] ... co ...
- Failed to chmod /Users/fei/Library/Developer/CoreSimulator/Devices/DB5AC3C0错误的解决办法
当XCode遇到此问题的时候,可通过重启模拟器和XCode来解决,拿走不谢
- 【BZOJ3555】企鹅QQ
蛤希. 用map会T. 只需要枚举删掉哪个字符,然后算出每个的hash值,sort一遍就行了. 用map会T!!! // It is made by XZZ #include<cstdio> ...
- Flutter - 创建横跨所有页面的侧滑菜单
前一篇博客讲到了如何创建侧滑菜单,但是再实际使用过程中,会发现,这个策划菜单只能在首页侧滑出来. 当导航到其他页面后,侧滑就不管用了.这也有点不符合良好的用户体验设计.Google Play就是很好的 ...
- (转载)jquery实现全选、反选、获得所有选中的checkbox
举了7个不同的checkbox状态,和大家一一分享. 1.全选 $("#btn1").click(function(){ $("input[name='checkbox' ...
- 用CSS3做3D动画的那些事
年会做了个3D变换的抽奖系统,在这里分享下通过CSS3制作3D效果的心得.抽奖系统虽然够炫酷,可惜抽的时候出了点bug,好几百人啊我的小心脏啊.虽然这个锅后面甩给会场的老爷电脑了(手动白眼). 首先介 ...
- C#/VB.NET与西门子PLC进行ModbusTcp通信
进入自动化行业半年多了,每天都与机器打交道. 前段时间接手一个任务,需要将我们机台与下站机台进行通讯,我们机台是PC,下站机台为西门子S7-1200. 想想完成这个任务,领导就会对我这个新人刮目相看, ...
- Python练习——矩形、直角三角形、乘法表
一.矩形 #******输入行和列,打印相应的矩形******# width = input("宽:") longth = input("长:") if (wi ...
- SpringBoot实现监听redis key失效事件
需求: 处理订单过期自动取消,比如下单30分钟未支付自动更改订单状态 解决方案1: 可以利用redis天然的key自动过期机制,下单时将订单id写入redis,过期时间30分钟,30分钟后检查订单状态 ...