Leetcode86. Partition List分隔链表(双指针)
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。
你应当保留两个分区中每个节点的初始相对位置。
示例:
输入: head = 1->4->3->2->5->2, x = 3 输出: 1->2->2->4->3->5
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* less_head = new ListNode(0);
ListNode* more_head = new ListNode(0);
ListNode* p = less_head;
ListNode* q = more_head;
while(head)
{
if(head ->val < x)
{
less_head ->next = head;
less_head = less_head ->next;
}
else
{
more_head ->next = head;
more_head = more_head ->next;
}
head = head ->next;
}
more_head ->next = NULL;
less_head ->next = q ->next;
return p ->next;
}
};
Leetcode86. Partition List分隔链表(双指针)的更多相关文章
- 086 Partition List 分隔链表
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前.你应当保留两个分区中每个节点的初始相对位置.例如,给定1->4->3->2-&g ...
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- Leetcode 86.分隔链表
分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...
- 【LeetCode】86. 分隔链表
86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- Java实现 LeetCode 725 分隔链表(暴力)
725. 分隔链表 给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分. 每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 nu ...
- [Swift]LeetCode86. 分隔链表 | Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【LeetCode-86】分隔链表
[题目描述] 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-& ...
- LeetCode 86. 分隔链表(Partition List)
题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...
随机推荐
- where与having区别
解释一. 聚合函数是比较where.having 的关键. 开门见山.where.聚合函数.having 在from后面的执行顺序: where>聚合函数(sum,min,max,avg,cou ...
- python基础语法(数据类型转换)
- wpf Rectangle
<Rectagle Width="100" Height="100" Stroke="Black" Fill="Blue&q ...
- iOS开发系列-文件下载
小文件下载 NSURLConnection下载小文件 #import "ViewController.h" @interface ViewController ()<NSUR ...
- 使用HttpStaus自定义返回状态
一.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- android ListView 获取点击的选项
需要调用listView的setOnItemClickListener方法 重写OnItemClickListener类的onItemClick 方法,onItemClick 方法有三个参数 @Ove ...
- 关于set_multicycle_path的最后总结
(1) –start/-end决定移动的距离以start_clock/end_clock为单元: (2) 对于-setup选项,移动距离是在默认关系的基础上移动(数值-1): (3) 默认往后, se ...
- string、char* 、int数据类型相互转换
string类型转换成char*类型,这里一般有以下三种方法: 1.c_str()方法 string name="Qian"; char *str=(char*)name.c_st ...
- LUOGU P3387 【模板】缩点 (缩点+DAG dp)
解题思路 缩点后按拓扑排序跑一个dp. #include<iostream> #include<cstdio> #include<cstring> #include ...
- 2019暑训第一场训练赛 |(2016-icpc区域赛)部分题解
// 今天下午比赛自闭了,晚上补了题,把AC的部分水题整理一下,记录坑点并吸取教训. // CF补题链接:http://codeforces.com/gym/101291 A - Alphabet 题 ...