-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPartitionList.py
More file actions
33 lines (29 loc) · 813 Bytes
/
PartitionList.py
File metadata and controls
33 lines (29 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 086. Partition List
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
"""
if not head:
return head
dummyLess, dummyLarge = ListNode(None) # 构造两个链表,分别存放小于和大于x的节点
less = dummyLess
large = dummyLarge
while head:
if head.val < x:
less.next = head
less = head
else:
large.next = head
large = head
head = head.next
# 将它们进行拼接
less.next = dummyLarge.next
large.next = None
return dummyLess.next