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 ...
随机推荐
- docker swarm mode routing mesh 使用
Docker Engine swarm mode makes it easy to publish ports for services to make them available to resou ...
- 解压RPM包
有时我们需要RPM包中的某个文件,如何解压RPM包呢? RPM包是使用cpio格式打包的,因此可以先转成cpio然后解压,如下所示: rpm2cpio xxx.rpm | cpio -div 例如: ...
- volatile浅析
volatile的意思是不稳定的.易变的,但在多线程中却跟字面意思一毛钱关系没有.作为一个变量修饰符,它主要有两个作用:一个是告诉大家,该变量是一个在多个线程之间均可见的变量:另一个是告诉java虚拟 ...
- 没有80端口的备案域名,如何做微信公众平台的开发?本文介绍可以通过任何域名来做开发,www.baidu.com和www.163.com和www.so.com这样的域名都可以
1.首先做过微信开发的朋友都知道,微信后台需要绑定80端口的备案域名,如果此时手上没有80端口的备案域名就不能进行开发了吗?当然不是 首先在这些地方绑定一个备案域名,国内公司的网址基本上是有备案的如w ...
- 基于标准库的string类实现简单的字符串替换
感觉基本功还是不扎实,虽然能做些程序但是现在看来我还是个初学者(primer),试着完成习题结果还得修修改改. 废话不多说,实现功能很简单,<C++ Primer>9.5.2节习题. // ...
- postman批量执行 要给请求加断言,批量执行的时候才会去统计,成功和失败的条数
1.设置请求断言后保存 2.点击runner去批量执行 3.有断言的请求就会统计
- 【BZOJ】2006: [NOI2010]超级钢琴(前缀和+RMQ+堆)
题目 传送门:QWQ 分析 又不会做....... 显然很好想到前缀和处理一下. 然后考虑最大化结果,直接上st表. 问题来了,然后呢? 怎么做$ length \in [l,r] $ 呢? 正解是 ...
- MySql——进阶一(库表的建 删 改)
基于学生管理系统建立 数据库 和 表 表的样式: 表(一)Student (学生use) 属性名 数据类型 可否为空 含 义 Sno varchar (20) 否 学号(主码) Sname varch ...
- python学习笔记(十一):网络编程
一.python操作网络,也就是打开一个网站,或者请求一个http接口,使用urllib模块. urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib ...
- Django学习---自定义分页
自定义分页 简单例子: urls.py: from django.contrib import admin from django.urls import path from django.conf. ...