leetcode 143. Reorder List 、86. Partition List
143. Reorder List
https://www.cnblogs.com/grandyang/p/4254860.html
先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接
class Solution {
public:
void reorderList(ListNode* head) {
if(head == NULL)
return;
ListNode* p1 = head;
ListNode* p2 = head;
while(p2->next != NULL && p2->next->next != NULL){
p1 = p1->next;
p2 = p2->next->next;
}
p2 = p1->next;
p1->next = NULL;
ListNode* pre = NULL;
while(p2 != NULL){
ListNode* tmp = p2->next;
p2->next = pre;
pre = p2;
p2 = tmp;
}
p1 = head;
p2 = pre;
while(p2 != NULL){
ListNode* tmp1 = p1->next;
ListNode* tmp2 = p2->next;
p1->next = p2;
p2->next = tmp1;
p1 = tmp1;
p2 = tmp2;
}
return;
}
};
86. Partition List
这个题和143有点相似,都是用两个指针,分别表示前面满足条件的最后一个,后面满足条件的最后一个。
但143的p2是只管当前指针,但86题p2是当前指针的前一个指针,因为86题需要判断是否满足小于x的条件,但143则不需要。
主循环那部分,只适合下一个指针大于x的情况,所以先通过一个循环找到第一个大于的x的前一个指针。
注意:要申请一个dummy指针保存最终的链表的返回,head指针也可能被排序排乱。
比如下面这种情况:
Input:
[2,1]
2
Output:
[2]
Expected:
[1,2]
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if(head == NULL)
return NULL;
ListNode* dummy = new ListNode(-);
dummy->next = head;
ListNode* pre = dummy;
while(pre->next && pre->next->val < x)
pre = pre->next;
ListNode* cur = pre;
while(cur->next){
if(cur->next->val >= x)
cur = cur->next;
else{
ListNode* tmp = cur->next;
cur->next = tmp->next;
tmp->next = pre->next;
pre->next = tmp;
pre = pre->next;
}
}
return dummy->next;
}
};
leetcode 143. Reorder List 、86. Partition List的更多相关文章
- 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 ...
- 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 ...
- Leetcode#143 Reorder List
原题地址 先把链表分割成前后两半,然后交叉融合 实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量.功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错. 代码: ListNode ...
- 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]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 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
- LeetCode:分割链表【86】
LeetCode:分割链表[86] 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例 ...
随机推荐
- nginx全局配置和性能优化
nginx目录结构和命令 1.ls /apps/nginx/: html是测试页,sbin是主程序 2.ls /apps/nginx/sbin/: nginx 只有一个程序文件 3. ...
- WCF之MSMQ消息队列
一.MSMQ简介 MSMQ(微软消息队列)是Windows操作系统中消息应用程序的基础,是用于创建分布式.松散连接的消息通讯应用程序的开发工具. MSMQ与XML Web Services和.Net ...
- hive 常用操作
参考:https://www.cnblogs.com/jonban/p/10779938.html Hive 启动:hive 退出:hive>quit; show databases; use ...
- socket.gaierror: [Errno 11001] getaddrinfo failed
- Define Interfaces and Share Class Members through Mixins in Dart
In this lesson, we will cover Interfaces and Mixins. Interfaces act as a contract containing propert ...
- mongodb WiredTiger 内存分配
转载自勤奋的小青蛙 mongodb占用内存非常高,这是因为官方为了提升存储的效率,设计就这么设计的. 但是大部分的个人开发者所购买的服务器内存并没有那么大,所以,我们需要配置下MongoDB的内存缓存 ...
- spring boot aop 切库实现读写分离
项目结构: 主要代码 : 配置数据库 配置datasource 线程隔离: 已上传git gitee地址:https://gitee.com/xxoo0_297/springboot-aop.git
- 正则re.complie作用
封装一个原本重复使用的正则表达式 prog = re.compile(pattern) result = prog.match(string)
- Hadoop(4)MapReduce 任务的推测(speculative)执行
Straggle(掉队者)是指那些跑的很慢但最终会成功完成的任务.一个掉队的Map任务会阻止Reduce任务开始执行. Hadoop不能自动纠正掉队任务,但是可以识别那些跑的比较慢的任务,然后它会产生 ...
- PHP 之去除代码中的注释
测试文件代码如下: <?php /** * Created by PhpStorm. * User: Yang * Date: 2019/10/16 * Time: 10:25 */ // 计算 ...