-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0086_partition_list.py
More file actions
111 lines (97 loc) · 2.95 KB
/
0086_partition_list.py
File metadata and controls
111 lines (97 loc) · 2.95 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#------------------------------------------------------------------------------
# Question: 0086_partition_list.py
#------------------------------------------------------------------------------
# tags: #linked_list
'''
Given a linked list and a value x, partition it such that all nodes less than
x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
'''
#------------------------------------------------------------------------------
# Solutions
#------------------------------------------------------------------------------
from typing import *
from test_utils.LinkedList import Node, LinkedList
class Solution:
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
Intuition: build two linked lists then merge at the end. (Stable)
"""
if head is None:
return
leftHead = Node(-1)
rightHead = Node(-1)
left = leftHead
right = rightHead
while head:
if head.data < x:
left.next = head
left = left.next
else:
right.next = head
right = right.next
head = head.next
left.next = rightHead.next
right.next = None
return leftHead
class Solution2:
def partition(self, head, x):
"""
Time: O(n)
Space: O(1)
Not stable
"""
node = head
new_head = head
new_tail = head
while node:
nextt = node.next
if node.data < x:
#add node to head
node.next = new_head
new_head = node
else:
#add node to end
new_tail.next = node
new_tail = node
node = nextt
new_tail.next = None
return new_head
#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------
import unittest
def isPartitioned(array, x):
toggle = False
n = len(array)
if n == 0 or n == 1:
return True
for i in range(1,n-1):
if array[i-1] < x and array[i] >= x:
if toggle:
return False
else:
toggle = True
return True
class TestSolution(unittest.TestCase):
def test_simple(self):
s = Solution()
ll = LinkedList([1,4,3,2,5,2])
head = ll.head
x = 3
s.partition(head, x)
self.assertTrue(isPartitioned(ll.to_array(), x))
def test_simple2(self):
s = Solution2()
ll = LinkedList([1,4,3,2,5,2])
head = ll.head
x = 3
s.partition(head, x)
self.assertTrue(isPartitioned(ll.to_array(), x))
unittest.main(verbosity=2)