Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70afb38

Browse files
新增66题: 分隔链表
1 parent 4508eb5 commit 70afb38

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @lc app=leetcode id=86 lang=cpp
3+
*
4+
* [86] Partition List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* ListNode *next;
13+
* ListNode() : val(0), next(nullptr) {}
14+
* ListNode(int x) : val(x), next(nullptr) {}
15+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
ListNode* partition(ListNode* head, int x) {
21+
ListNode less_g;
22+
ListNode greater_g;
23+
ListNode *less = &less_g;
24+
ListNode *greater = &greater_g;
25+
26+
while (head != nullptr)
27+
{
28+
if (head->val >= x)
29+
{
30+
greater->next = head;
31+
greater = greater->next;
32+
}
33+
else
34+
{
35+
less->next = head;
36+
less = less->next;
37+
}
38+
head = head->next;
39+
}
40+
greater->next = nullptr;
41+
less->next = greater_g.next;
42+
return less_g.next;
43+
}
44+
};
45+
// @lc code=end
46+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 分隔链表
2+
3+
#### *Patition List*
4+
5+
给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。
6+
7+
你应当 保留 两个分区中每个节点的初始相对位置。
8+
9+
10+
11+
12+
英文题目:
13+
14+
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`.
15+
16+
You should **preserve** the original relative order of the nodes in each of the two partitions.
17+
18+
19+
20+
**Constraints:**
21+
22+
- The number of nodes in the list is in the range `[0, 200]`.
23+
- `-100 <= Node.val <= 100`
24+
- `-200 <= x <= 200`
25+
26+
**example 1**
27+
28+
![example 1](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/66.%20Patition%20List(Medium)/patition.jpg)
29+
30+
```
31+
Input: head = [1,4,3,2,5,2], x = 3
32+
Output: [1,2,2,4,3,5]
33+
```
34+
35+
**example 2**
36+
37+
```
38+
Input: head = [2,1], x = 2
39+
Output: [1,2]
40+
```
41+
42+
43+
44+
---
45+
46+
### 思路
47+
48+
1. 使用`less``greater`两个指针,遍历链表时,大于等于`x`的元素插入在`less`后,小于等于`x`的插入`greater`后,完成遍历后,将`greater`插入`less`后即可
49+
2. 避免链表出现环,注意处理`greater`,遍历完成后让`greater->next = nullptr`
50+
51+
52+
### 代码
53+
```cpp
54+
55+
/*
56+
* @lc app=leetcode id=86 lang=cpp
57+
*
58+
* [86] Partition List
59+
*/
60+
61+
// @lc code=start
62+
/**
63+
* Definition for singly-linked list.
64+
* struct ListNode {
65+
* int val;
66+
* ListNode *next;
67+
* ListNode() : val(0), next(nullptr) {}
68+
* ListNode(int x) : val(x), next(nullptr) {}
69+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
70+
* };
71+
*/
72+
class Solution {
73+
public:
74+
ListNode* partition(ListNode* head, int x) {
75+
ListNode less_g;
76+
ListNode greater_g;
77+
ListNode *less = &less_g;
78+
ListNode *greater = &greater_g;
79+
80+
while (head != nullptr)
81+
{
82+
if (head->val >= x)
83+
{
84+
greater->next = head;
85+
greater = greater->next;
86+
}
87+
else
88+
{
89+
less->next = head;
90+
less = less->next;
91+
}
92+
head = head->next;
93+
}
94+
greater->next = nullptr;
95+
less->next = greater_g.next;
96+
return less_g.next;
97+
}
98+
};
99+
// @lc code=end
100+
```
101+
102+
本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode)
23.2 KB
Loading[フレーム]

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@
6666

6767
##### 65. [83.Remove Duplicates from Sorted List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/65.%20Remove%20Duplicates%20from%20Sorted%20List(Easy)) 删除排序链表中的重复元素
6868

69+
##### 65. [86.Partition List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/66.%20Patition%20List(Medium)) 分隔链表

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /