-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path86.py
67 lines (57 loc) · 1.54 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
66
67
class ListNode():
def __init__(self, val, next=None):
self.val = val
self.next = next
def create_linked_list(arr):
head = ListNode(arr[0])
cur = head
for i in range(1, len(arr)):
cur.next = ListNode(arr[i])
cur = cur.next
return head
def print_linked_list(head):
cur = head
res = []
while cur:
res.append(cur.val)
cur = cur.next
return res
class Solution():
def splitNodeList(self, head, x):
p = head
p_high = ListNode(0)
p_low = ListNode(0)
new_head = p_low
mew_head_ = p_high
while p:
if p.val < x:
p_low.next = p
p_low = p_low.next
else:
p_high.next = p
p_high = p_high.next
p = p.next
p_low.next = mew_head_.next
return new_head.next
class Solution2():
def splitNodeList(self, head,x):
head_low = ListNode(-1)
low_p = head_low
head_high = ListNode(-1)
high_p = head_high
p = head
while p is not None:
if p.val < x:
low_p.next = p
low_p = low_p.next
else:
high_p.next = p
high_p = high_p.next
p = p.next
low_p.next = head_high.next
return head_low.next
if __name__ == "__main__":
solu = Solution()
head = create_linked_list([1, 5, 8, 4, 5, 1, 2, 9, 7])
new_head = solu.splitNodeList(head, 7)
print(print_linked_list(new_head))