Skip to content

Commit 027c931

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 02.04. 分割链表
1 parent b6ac462 commit 027c931

File tree

4 files changed

+208
-35
lines changed

4 files changed

+208
-35
lines changed

lcci/02.04.Partition List/README.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,74 @@
1313

1414
## 解法
1515
<!-- 这里可写通用的实现逻辑 -->
16-
16+
创建两个链表,一个存放小于 `x` 的节点,另一个存放大于等于 `x` 的节点,之后进行拼接即可。
1717

1818
### Python3
1919
<!-- 这里可写当前语言的特殊实现逻辑 -->
2020

2121
```python
22+
# Definition for singly-linked list.
23+
# class ListNode:
24+
# def __init__(self, x):
25+
# self.val = x
26+
# self.next = None
2227

28+
class Solution:
29+
def partition(self, head: ListNode, x: int) -> ListNode:
30+
if head is None or head.next is None:
31+
return head
32+
left, right = ListNode(-1), ListNode(-1)
33+
p, q = left, right
34+
while head:
35+
t = head.next
36+
head.next = None
37+
if head.val < x:
38+
p.next = head
39+
p = p.next
40+
else:
41+
q.next = head
42+
q = q.next
43+
head = t
44+
p.next = right.next
45+
return left.next
2346
```
2447

2548
### Java
2649
<!-- 这里可写当前语言的特殊实现逻辑 -->
2750

2851
```java
29-
52+
/**
53+
* Definition for singly-linked list.
54+
* public class ListNode {
55+
* int val;
56+
* ListNode next;
57+
* ListNode(int x) { val = x; }
58+
* }
59+
*/
60+
class Solution {
61+
public ListNode partition(ListNode head, int x) {
62+
if (head == null || head.next == null) {
63+
return head;
64+
}
65+
ListNode left = new ListNode(-1);
66+
ListNode right = new ListNode(-1);
67+
ListNode p = left, q = right;
68+
while (head != null) {
69+
ListNode t = head.next;
70+
head.next = null;
71+
if (head.val < x) {
72+
p.next = head;
73+
p = p.next;
74+
} else {
75+
q.next = head;
76+
q = q.next;
77+
}
78+
head = t;
79+
}
80+
p.next = right.next;
81+
return left.next;
82+
}
83+
}
3084
```
3185

3286
### ...
+95-33
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,95 @@
1-
# [02.04. Partition List](https://leetcode-cn.com/problems/partition-list-lcci)
2-
3-
## Description
4-
<p>Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the &quot;right partition&quot;; it does not need to appear between the left and right partitions.</p>
5-
6-
<p><strong>Example:</strong></p>
7-
8-
<pre>
9-
<strong>Input:</strong> head = 3-&gt;5-&gt;8-&gt;5-&gt;10-&gt;2-&gt;1, <em>x</em> = 5
10-
<strong>Output:</strong> 3-&gt;1-&gt;2-&gt;10-&gt;5-&gt;5-&gt;8
11-
</pre>
12-
13-
14-
15-
## Solutions
16-
17-
18-
### Python3
19-
20-
```python
21-
22-
```
23-
24-
### Java
25-
26-
```java
27-
28-
```
29-
30-
### ...
31-
```
32-
33-
```
1+
# [02.04. Partition List](https://leetcode-cn.com/problems/partition-list-lcci)
2+
3+
## Description
4+
<p>Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the &quot;right partition&quot;; it does not need to appear between the left and right partitions.</p>
5+
6+
7+
8+
<p><strong>Example:</strong></p>
9+
10+
11+
12+
<pre>
13+
14+
<strong>Input:</strong> head = 3-&gt;5-&gt;8-&gt;5-&gt;10-&gt;2-&gt;1, <em>x</em> = 5
15+
16+
<strong>Output:</strong> 3-&gt;1-&gt;2-&gt;10-&gt;5-&gt;5-&gt;8
17+
18+
</pre>
19+
20+
21+
22+
23+
## Solutions
24+
25+
26+
### Python3
27+
28+
```python
29+
# Definition for singly-linked list.
30+
# class ListNode:
31+
# def __init__(self, x):
32+
# self.val = x
33+
# self.next = None
34+
35+
class Solution:
36+
def partition(self, head: ListNode, x: int) -> ListNode:
37+
if head is None or head.next is None:
38+
return head
39+
left, right = ListNode(-1), ListNode(-1)
40+
p, q = left, right
41+
while head:
42+
t = head.next
43+
head.next = None
44+
if head.val < x:
45+
p.next = head
46+
p = p.next
47+
else:
48+
q.next = head
49+
q = q.next
50+
head = t
51+
p.next = right.next
52+
return left.next
53+
```
54+
55+
### Java
56+
57+
```java
58+
/**
59+
* Definition for singly-linked list.
60+
* public class ListNode {
61+
* int val;
62+
* ListNode next;
63+
* ListNode(int x) { val = x; }
64+
* }
65+
*/
66+
class Solution {
67+
public ListNode partition(ListNode head, int x) {
68+
if (head == null || head.next == null) {
69+
return head;
70+
}
71+
ListNode left = new ListNode(-1);
72+
ListNode right = new ListNode(-1);
73+
ListNode p = left, q = right;
74+
while (head != null) {
75+
ListNode t = head.next;
76+
head.next = null;
77+
if (head.val < x) {
78+
p.next = head;
79+
p = p.next;
80+
} else {
81+
q.next = head;
82+
q = q.next;
83+
}
84+
head = t;
85+
}
86+
p.next = right.next;
87+
return left.next;
88+
}
89+
}
90+
```
91+
92+
### ...
93+
```
94+
95+
```
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode partition(ListNode head, int x) {
11+
if (head == null || head.next == null) {
12+
return head;
13+
}
14+
ListNode left = new ListNode(-1);
15+
ListNode right = new ListNode(-1);
16+
ListNode p = left, q = right;
17+
while (head != null) {
18+
ListNode t = head.next;
19+
head.next = null;
20+
if (head.val < x) {
21+
p.next = head;
22+
p = p.next;
23+
} else {
24+
q.next = head;
25+
q = q.next;
26+
}
27+
head = t;
28+
}
29+
p.next = right.next;
30+
return left.next;
31+
}
32+
}

lcci/02.04.Partition List/Solution.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
8+
class Solution:
9+
def partition(self, head: ListNode, x: int) -> ListNode:
10+
if head is None or head.next is None:
11+
return head
12+
left, right = ListNode(-1), ListNode(-1)
13+
p, q = left, right
14+
while head:
15+
t = head.next
16+
head.next = None
17+
if head.val < x:
18+
p.next = head
19+
p = p.next
20+
else:
21+
q.next = head
22+
q = q.next
23+
head = t
24+
p.next = right.next
25+
return left.next

0 commit comments

Comments
 (0)