Skip to content

Commit 36b3ae5

Browse files
Add files via upload
1 parent 19801b4 commit 36b3ae5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Partition List/Partition_List.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 第一种思路,简单粗暴,使用两个列表,一个存放小节点,一个存放大节点
2+
# 44ms 77.50%
3+
class Solution:
4+
def partition(self, head, x):
5+
"""
6+
:type head: ListNode
7+
:type x: int
8+
:rtype: ListNode
9+
"""
10+
smaller_stack, greater_stack = [], []
11+
while head is not None:
12+
if head.val < x:
13+
smaller_stack.append(head)
14+
else:
15+
greater_stack.append(head)
16+
head = head.next
17+
for index in range(len(smaller_stack) - 1):
18+
smaller_stack[index].next = smaller_stack[index + 1]
19+
for index in range(len(greater_stack) - 1):
20+
greater_stack[index].next = greater_stack[index + 1]
21+
22+
if smaller_stack:
23+
head = smaller_stack[0]
24+
if greater_stack:
25+
smaller_stack[-1].next = greater_stack[0]
26+
greater_stack[-1].next = None
27+
else:
28+
smaller_stack[-1].next = None
29+
else:
30+
if greater_stack:
31+
head = greater_stack[0]
32+
greater_stack[-1].next = None
33+
else:
34+
return None
35+
return head

0 commit comments

Comments
 (0)