143. Reorder List(List)
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}
.
class Solution {
public:
void reorderList(ListNode *head) {
if (!head|| !head->next)
return; // partition the list into 2 sublists of equal length
ListNode *slowNode = head, *fastNode = head;
while (fastNode->next) {
fastNode = fastNode->next;
if (fastNode->next) {
fastNode = fastNode->next;
} else {
break;
}
slowNode = slowNode->next;
}
// 2 sublist heads
ListNode *head1 = head, *head2 = slowNode->next;
// detach the two sublists
slowNode->next = NULL; // reverse the second sublist
ListNode *cur = head2, *post = cur->next;
cur->next = NULL;
while (post) {
ListNode* temp = post->next;
post->next = cur;
cur = post;
post = temp;
}
head2 = cur; // the new head of the reversed sublist // merge the 2 sublists as required
ListNode* p = head1, *q = head2;
while (q ) {
ListNode *temp1 = p->next;
ListNode *temp2 = q->next;
p->next = q;
q->next = temp1;
p = temp1;
q = temp2;
}
}
};
143. Reorder List(List)的更多相关文章
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
- Java for LeetCode 143 Reorder List
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 th ...
- 143. Reorder List
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 thi ...
- leetcode 143. Reorder List ----- java
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 thi ...
- Leetcode 143. Reorder List(Medium)
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 thi ...
- LeetCode OJ 143. Reorder List(两种方法,快慢指针,堆栈)
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 thi ...
- [leetcode]143. Reorder List重排链表
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...
- 【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 ...
随机推荐
- luvit 被忽视的lua 高性能框架(仿nodejs)
备注: luvit 开放模式和nodejs 一样,但是因为生态以及小众语言的问题,使用的人比较少,但是从目前 来看更新速度还是比较快的,但是从现有lua 开发框架来说一般倾向于使 ...
- yum命令集
升级相关命令: yum update : 安装所有更新软件 yum update xxx : 仅更新指定的软件 yum check-update : 列出所有可更新的软件清单 yum list : 列 ...
- RedHat6.5用ISO配置yum源
CentOS自带强大的yum功能,默认为从网上自动下载rpm包,对于网速不太给力或者没有网络的情况下需要用的话就不是很方便,很多软件尤其是服务器上的软件我们么有必要追求最新,稳定性最重要,这里我们用C ...
- Cannot read property 'setState' of undefined
You're using function() in your Promise chain, this will change the scope for this. If you're using ...
- beego的配置文件记录
摘自https://github.com/beego/tutorial/blob/master/zh/3/params.slide * beego的默认参数 - AppName 应用名称,默认是 be ...
- Cousera 无法播放视频 解决办法 widows 和 linux
查资料得知,Cousera无法播放课程视频原因在于DNS污染. 尽管通过FQ软件把视频看完了,在最后一课找到了这个解决办法,现在拿出来分享给大家: Windows: 请参照以下链接: http://j ...
- java代码----------计算器代码
总结: 很多不完善—— package com.rue; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.a ...
- mybatis实现继承映射
ORM 框架的优势在于能让我们利用面向对象的思维去操作数据库, hibernate 作为重量级的 ORM 框架对面向对象的支持很强大.作为半自动化的 mybatis ,对面向对象的支持也是很完备的.这 ...
- ansible之感冒药
Ansible简介安装 Ansible是一个综合的强大的管理工具,他可以对多台主机安装操作系统,并为这些主机安装不同的应用程序,也可以通知指挥这些主机完成不同的任务.查看多台主机的各种信息的状态等,a ...
- MySQL Connector/NET 使用小结(踩坑之路)
背景描述 根据项目的需要,需连接MySQL获取数据. 首先,先了解一下项目的情况: 之前的代码是C#编写的的, 运行时:.NETFramework3.5. 由于项目已经部署上线,因此不能升级运行时,这 ...