Skip to content

Commit 007e450

Browse files
committed
regular leetcode
1 parent e394fcc commit 007e450

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

086._partition_list.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
###86. Partition List
2+
3+
4+
题目:
5+
<https://leetcode.com/problems/partition-list/>
6+
7+
8+
难度 : Medium
9+
10+
11+
思路一:
12+
13+
14+
最简单的思路就是两个dummy head,然后一个指向 小于的node,一个指向大于的node
15+
16+
17+
思路二:
18+
19+
不走寻常路了,使用两个指针,一个指向小于的尾巴,一个一直往后走,指向大于,然后交换node
20+
21+
完成比完美更重要啊,其实可以先试试用简单方法,因为我用我的不走寻常路画了比较久的图,写起来也稍显没那么美观,还在交换node的部分卡了一会
22+
23+
24+
25+
```
26+
class Solution(object):
27+
def partition(self, head, x):
28+
"""
29+
:type head: ListNode
30+
:type x: int
31+
:rtype: ListNode
32+
"""
33+
dummy = ListNode(-1)
34+
dummy.next = head
35+
36+
p1 = p2 = dummy
37+
38+
while p1.next and p1.next.val < x:
39+
p1 = p1.next
40+
41+
p2 = p1.next
42+
43+
while p2:
44+
while p2.next and p2.next.val >= x:
45+
p2 = p2.next
46+
47+
if p2.next == None:
48+
break
49+
node = p2.next
50+
p2.next = node.next
51+
node.next = p1.next
52+
p1.next = node
53+
p1 = p1.next
54+
55+
return dummy.next
56+
```

0 commit comments

Comments
 (0)