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 ...
随机推荐
- jdk、jre、JVM的简单区别与联系
2015-10-20 23:08:52 (1)jdk Java development toolkit(开发工具包),JDK是整个JAVA的核心,包括了Java运行环境jre(Java Runtime ...
- 理解AI的角度
<经济学人>去年出了一期很经典的封面,封面里将全球各大高科技平台企业如谷歌.亚马逊之许描绘成正在采油的钻井,寓意很明显,在数字经济时代,大平台正在开采数字化的石油——大数据,而开采出来的大 ...
- 【转】深入 Python :Dive Into Python 中文版
原文网址:http://woodpecker.org.cn/diveintopython/power_of_introspection/lambda_functions.html 4.7. 使用 la ...
- Linux环境安装配置Swftools
系统:CentOS6.5的64位版本 这里有一位仁兄的几个错误处理办法,下面是swftools的安装配置步骤: 1.安装所需的库和组件.机器之前安装过了,主要安装的是下面几个组件.如果不安装会 ...
- 扩充 jQuery EasyUI Datagrid 数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
客户需求: jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) 如图所示,Datagrid 鼠标悬停/离开数据行时 ...
- MVC涉及RouteTable自定义路径
Routing 到目前为止,我们已经解决了MVC的很多问题,但忽略了最基本的最重要的一个问题:当用户发送请求时,会发生什么? 最好的答案是“执行Action 方法”,但仍存在疑问:对于一个特定的URL ...
- openresty+lua做接口调用权限限制
说明:openresty可以理解为一个服务器它将nginx的核心包含了过来,并结合lua脚本语言实现一些对性能要求高的功能,该篇文章介绍了使用openresty 1.purview.lua --调用j ...
- python 多线程要点
要点整理 多线程 #coding=utf-8 import threading from time import ctime,sleep def music(func): for i in range ...
- i和j的值交换的方法
方法一: int i = 3, j = 5; int c = i; i = j; j = c; 方法二: int i = 3, j = 5; int n = i + j; i = n ...
- Java堆外内存之一:堆外内存场景介绍(对象池VS堆外内存)
最近经常有人问我在Java中使用堆外(off heap)内存的好处与用途何在.我想其他面临几样选择的人应该也会对这个答案感兴趣吧. 堆外内存其实并无特别之处.线程栈,应用程序代码,NIO缓存用的都是堆 ...