Skip to content

Commit ca066db

Browse files
authored
feat: add solutions to lc problem: No.0086 (#3963)
No.0086.Partition List
1 parent 9a0b0db commit ca066db

File tree

10 files changed

+401
-216
lines changed

10 files changed

+401
-216
lines changed

solution/0000-0099/0086.Partition List/README.md

+137-72
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ tags:
5555

5656
### 方法一:模拟
5757

58-
我们创建两个链表,一个存放小于 $x$ 的节点,另一个存放大于等于 $x$ 的节点,之后进行拼接即可
58+
我们创建两个链表 $l$ 和 $r$,一个用来存储小于 $x$ 的节点,另一个用来存储大于等于 $x$ 的节点。然后我们将它们拼接起来
5959

60-
时间复杂度 $O(n),其中 $n$ 是原链表的长度。空间复杂度 $O(1)$。
60+
时间复杂度 $O(n)$,其中 $n$ 是原链表的长度。空间复杂度 $O(1)$。
6161

6262
<!-- tabs:start -->
6363

@@ -71,19 +71,20 @@ tags:
7171
# self.next = next
7272
class Solution:
7373
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
74-
d1, d2 = ListNode(), ListNode()
75-
t1, t2 = d1, d2
74+
l = ListNode()
75+
r = ListNode()
76+
tl, tr = l, r
7677
while head:
7778
if head.val < x:
78-
t1.next = head
79-
t1 = t1.next
79+
tl.next = head
80+
tl = tl.next
8081
else:
81-
t2.next = head
82-
t2 = t2.next
82+
tr.next = head
83+
tr = tr.next
8384
head = head.next
84-
t1.next = d2.next
85-
t2.next = None
86-
return d1.next
85+
tr.next = None
86+
tl.next = r.next
87+
return l.next
8788
```
8889

8990
#### Java
@@ -101,22 +102,21 @@ class Solution:
101102
*/
102103
class Solution {
103104
public ListNode partition(ListNode head, int x) {
104-
ListNode d1 = new ListNode();
105-
ListNode d2 = new ListNode();
106-
ListNode t1 = d1, t2 = d2;
107-
while (head != null) {
105+
ListNode l = new ListNode();
106+
ListNode r = new ListNode();
107+
ListNode tl = l, tr = r;
108+
for (; head != null; head = head.next) {
108109
if (head.val < x) {
109-
t1.next = head;
110-
t1 = t1.next;
110+
tl.next = head;
111+
tl = tl.next;
111112
} else {
112-
t2.next = head;
113-
t2 = t2.next;
113+
tr.next = head;
114+
tr = tr.next;
114115
}
115-
head = head.next;
116116
}
117-
t1.next = d2.next;
118-
t2.next = null;
119-
return d1.next;
117+
tr.next = null;
118+
tl.next = r.next;
119+
return l.next;
120120
}
121121
}
122122
```
@@ -137,23 +137,22 @@ class Solution {
137137
class Solution {
138138
public:
139139
ListNode* partition(ListNode* head, int x) {
140-
ListNode* d1 = new ListNode();
141-
ListNode* d2 = new ListNode();
142-
ListNode* t1 = d1;
143-
ListNode* t2 = d2;
144-
while (head) {
140+
ListNode* l = new ListNode();
141+
ListNode* r = new ListNode();
142+
ListNode* tl = l;
143+
ListNode* tr = r;
144+
for (; head; head = head->next) {
145145
if (head->val < x) {
146-
t1->next = head;
147-
t1 = t1->next;
146+
tl->next = head;
147+
tl = tl->next;
148148
} else {
149-
t2->next = head;
150-
t2 = t2->next;
149+
tr->next = head;
150+
tr = tr->next;
151151
}
152-
head = head->next;
153152
}
154-
t1->next = d2->next;
155-
t2->next = nullptr;
156-
return d1->next;
153+
tr->next = nullptr;
154+
tl->next = r->next;
155+
return l->next;
157156
}
158157
};
159158
```
@@ -169,21 +168,53 @@ public:
169168
* }
170169
*/
171170
func partition(head *ListNode, x int) *ListNode {
172-
d1, d2 := &ListNode{}, &ListNode{}
173-
t1, t2 := d1, d2
174-
for head != nil {
171+
l, r := &ListNode{}, &ListNode{}
172+
tl, tr := l, r
173+
for ; head != nil; head = head.Next {
175174
if head.Val < x {
176-
t1.Next = head
177-
t1 = t1.Next
175+
tl.Next = head
176+
tl = tl.Next
178177
} else {
179-
t2.Next = head
180-
t2 = t2.Next
178+
tr.Next = head
179+
tr = tr.Next
181180
}
182-
head = head.Next
183181
}
184-
t1.Next = d2.Next
185-
t2.Next = nil
186-
return d1.Next
182+
tr.Next = nil
183+
tl.Next = r.Next
184+
return l.Next
185+
}
186+
```
187+
188+
#### TypeScript
189+
190+
```ts
191+
/**
192+
* Definition for singly-linked list.
193+
* class ListNode {
194+
* val: number
195+
* next: ListNode | null
196+
* constructor(val?: number, next?: ListNode | null) {
197+
* this.val = (val===undefined ? 0 : val)
198+
* this.next = (next===undefined ? null : next)
199+
* }
200+
* }
201+
*/
202+
203+
function partition(head: ListNode | null, x: number): ListNode | null {
204+
const [l, r] = [new ListNode(), new ListNode()];
205+
let [tl, tr] = [l, r];
206+
for (; head; head = head.next) {
207+
if (head.val < x) {
208+
tl.next = head;
209+
tl = tl.next;
210+
} else {
211+
tr.next = head;
212+
tr = tr.next;
213+
}
214+
}
215+
tr.next = null;
216+
tl.next = r.next;
217+
return l.next;
187218
}
188219
```
189220

@@ -208,22 +239,24 @@ func partition(head *ListNode, x int) *ListNode {
208239
// }
209240
impl Solution {
210241
pub fn partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
211-
let mut head = head;
212-
let mut d1 = Some(Box::new(ListNode::new(0)));
213-
let mut d2 = Some(Box::new(ListNode::new(0)));
214-
let (mut t1, mut t2) = (&mut d1, &mut d2);
215-
while let Some(mut node) = head {
216-
head = node.next.take();
242+
let mut l = ListNode::new(0);
243+
let mut r = ListNode::new(0);
244+
let mut tl = &mut l;
245+
let mut tr = &mut r;
246+
let mut current = head;
247+
while let Some(mut node) = current {
248+
current = node.next.take();
217249
if node.val < x {
218-
t1.as_mut().unwrap().next = Some(node);
219-
t1 = &mut t1.as_mut().unwrap().next;
250+
tl.next = Some(node);
251+
tl = tl.next.as_mut().unwrap();
220252
} else {
221-
t2.as_mut().unwrap().next = Some(node);
222-
t2 = &mut t2.as_mut().unwrap().next;
253+
tr.next = Some(node);
254+
tr = tr.next.as_mut().unwrap();
223255
}
224256
}
225-
t1.as_mut().unwrap().next = d2.unwrap().next;
226-
d1.unwrap().next
257+
tr.next = None;
258+
tl.next = r.next;
259+
l.next
227260
}
228261
}
229262
```
@@ -244,26 +277,58 @@ impl Solution {
244277
* @return {ListNode}
245278
*/
246279
var partition = function (head, x) {
247-
const d1 = new ListNode();
248-
const d2 = new ListNode();
249-
let t1 = d1,
250-
t2 = d2;
251-
while (head) {
280+
const [l, r] = [new ListNode(), new ListNode()];
281+
let [tl, tr] = [l, r];
282+
for (; head; head = head.next) {
252283
if (head.val < x) {
253-
t1.next = head;
254-
t1 = t1.next;
284+
tl.next = head;
285+
tl = tl.next;
255286
} else {
256-
t2.next = head;
257-
t2 = t2.next;
287+
tr.next = head;
288+
tr = tr.next;
258289
}
259-
head = head.next;
260290
}
261-
t1.next = d2.next;
262-
t2.next = null;
263-
return d1.next;
291+
tr.next = null;
292+
tl.next = r.next;
293+
return l.next;
264294
};
265295
```
266296

297+
#### C#
298+
299+
```cs
300+
/**
301+
* Definition for singly-linked list.
302+
* public class ListNode {
303+
* public int val;
304+
* public ListNode next;
305+
* public ListNode(int val=0, ListNode next=null) {
306+
* this.val = val;
307+
* this.next = next;
308+
* }
309+
* }
310+
*/
311+
public class Solution {
312+
public ListNode Partition(ListNode head, int x) {
313+
ListNode l = new ListNode();
314+
ListNode r = new ListNode();
315+
ListNode tl = l, tr = r;
316+
for (; head != null; head = head.next) {
317+
if (head.val < x) {
318+
tl.next = head;
319+
tl = tl.next;
320+
} else {
321+
tr.next = head;
322+
tr = tr.next;
323+
}
324+
}
325+
tr.next = null;
326+
tl.next = r.next;
327+
return l.next;
328+
}
329+
}
330+
```
331+
267332
<!-- tabs:end -->
268333

269334
<!-- solution:end -->

0 commit comments

Comments
 (0)