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 ...
随机推荐
- fabio 安装试用&&实际使用的几个问题
备注: 因为fabio 依赖consul vault (不是强需),启动之前需要先安装consul, 本次为了简单consul 使用的是单机,使用的是dev 模式 1. conusl ...
- Python中多维数组flatten的技巧
res00是一张rgb图 [x for sub1 in res00 for sub2 in sub1 for x in sub2] 列出所有像素值
- python 网络编程--socket模块/struct模块
socket模块: 客户端:CS架构, client -> server 浏览器:BS架构, browser -> server 网络通信本质:传输字节 doc命令查看ip地址:ipc ...
- postman md5加密例子
引用方法 {{md5}} 参考:https://www.jianshu.com/p/5a49d1deaf69 实例:
- 阻塞队列之二:LinkedTransferQueue
一.LinkedTransferQueue简介 TransferQueue是一个继承了BlockingQueue的接口,并且增加若干新的方法.LinkedTransferQueue是TransferQ ...
- python的数据类型,数字,布尔,字符串
1.数字 数字过大,类型会自动变化,这个变化是python2特有的,在python3里都是int,不区分int和long float(浮点型),也就是小数 complex(复数) 2.布尔值 真或者假 ...
- 二十二 synchronized同步方法
一 Synchronized锁: 1 synchronized取得的锁都是对象锁,而不是把一段代码或方法加锁. synchronized是给该方法的实例对象加锁.如果多个线程访问的是同一个对象 的s ...
- python学习(二十三) String(下) 分片和索引
分片: 记住, 是开闭区间. a = "abcdef"print(a[:])print(a[1:])print(a[:3])print(a[-2])print(a[:-2])pri ...
- 【UVA】10391 Compound Words(STL map)
题目 题目 分析 自认已经很简洁了,虽说牺牲了一些效率 代码 #include <bits/stdc++.h> using namespace std; set <s ...
- SQL Server 2008系统信息查询常用命令 查看表大小、记录数等
1.返回所有数据库信息(数据库名,创建日期,存储路径等). use master; GO select * from dbo.sysdatabases 2.返回当前数据库所有对象(可根据type字 ...