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 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例 ...
随机推荐
- c# 处理串行化对象的版本变化
- [postman][API 测试]用Postman做RestAPI测试学习笔记
痛点:最近有个API网关的兼容性测试任务,需要验证API是否可用,返回值符合预期,如果手工复制粘贴curl命令,繁琐且低效 调研时发现了Postman 这个chrom插件,试用了2天后发现使用起来很方 ...
- Linux的进程管理基本指令
在Linux操作系统中,进程是指一个程序的运行实例,它需要存储器来存储程序本身及其操作数据.内核负责创建和跟踪进程.当程序运行时,内核首先准备好一些内存,将可执行代码从文件系统加载到内存里,然后开始运 ...
- Nginx- web服务配置与测试
(一) 软件介绍由俄罗斯人lgor Sysove开发,为开源软件.支持高并发:支持几万并发连接(特别是静态小文件业务环境) 资源消耗少:在3万并发连接下开启10个Nginx线程消耗内存不到200M 支 ...
- canvas圆形进度条(逆时针)
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- prometheus监控redis,redis-cluster
Prometheus监控redis使用的是redis_exporter, 作者GitHub: https://github.com/oliver006/redis_exporter 需要说明的是: r ...
- vulkan load store and memoryless
https://www.jendrikillner.com/article_database/ https://community.arm.com/developer/tools-software/g ...
- KM 最大权匹配 UVA 1411/POJ 3565
#include <bits/stdc++.h> using namespace std; inline void read(int &num) { char ch; num = ...
- BZOJ 3601 一个人的数论 (拉格朗日插值+莫比乌斯反演)
题意 略 题解 orz Freopen的博客 CODE #pragma GCC optimize (3) #include <bits/stdc++.h> using namespace ...
- 01_第一次如何上传GitHub(转)Updates were rejected because the tip of your current branch is behind
https://www.cnblogs.com/code-changeworld/p/4779145.html 刚创建的github版本库,在push代码时出错: $ git push -u orig ...