-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartitionList.py
52 lines (51 loc) · 1.32 KB
/
PartitionList.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
"""
if head is None:
return None
leftDummy = ListNode(0)
left = leftDummy
rightDummy = ListNode(0)
right = rightDummy
node = head
while node:
if node.val < x:
left.next = node
left = left.next
else:
right.next = node
right = right.next
node = node.next
right.next = None
left.next = rightDummy.next
return leftDummy.next
# if head is None or head.next is None:
# return head
# dummy = ListNode(0)
# dummy.next = head
# p = dummy
# low = p
# while p.next:
# if p.next.val < x:
# if low.next == p.next:
# p = p.next
# low = low.next
# else:
# tmp1 = low.next
# tmp2 = p.next.next
# low.next = p.next
# low.next.next = tmp1
# p.next = tmp2
# low = low.next
# else:
# p = p.next
# return dummy.next