File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments