-
Notifications
You must be signed in to change notification settings - Fork 0
/
86.py
65 lines (55 loc) · 1.5 KB
/
86.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
53
54
55
56
57
58
59
60
61
62
63
64
65
# Definition for singly-linked list.
# remember add a head node
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Li(object):
def __init__(self, l):
self.head = ListNode(l[0])
cur = self.head
for i in range(1, len(l)):
cur.next = ListNode(l[i])
cur = cur.next
def returnHead(self):
return self.head
def printL(self, head):
cur = head
while cur:
print cur.val,'->',
cur = cur.next
class Solution(object):
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
"""
L = ListNode(x-1)
L.next = head
head = L
cur = head
while cur:
while cur.next and cur.next.val<x:
cur = cur.next
if cur.next == None:
return head.next
next = cur.next
while next.next and next.next.val>=x:
next = next.next
if next.next == None:
return head.next
q = next.next
next.next = next.next.next
temp = cur.next
cur.next = q
cur = cur.next
cur.next = temp
return head.next
if __name__ == '__main__':
solution = Solution()
li = Li([2,3,1])
head = li.returnHead()
print li.printL(head)
head = solution.partition(head, 2)
print li.printL(head)