10
10
not need to appear between the left and right
11
11
partitions.
12
12
EXAMPLE
13
- Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition = 5]
13
+ Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [pivot = 5]
14
14
Result: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
15
15
"""
16
16
import unittest
@@ -94,7 +94,7 @@ def __eq__(self, other: object):
94
94
return a is None and b is None
95
95
96
96
97
- def check_partitioned (ll : LinkedList , partition : int ) -> bool :
97
+ def check_partitioned (ll : LinkedList , pivot : int ) -> bool :
98
98
"""
99
99
Will check if a linked list is partitioned
100
100
around a value such that all values less
@@ -106,31 +106,30 @@ def check_partitioned(ll: LinkedList, partition: int) -> bool:
106
106
Runtime: O(n)
107
107
Space Complexity: O(1)
108
108
:param ll: an input linked list
109
- :param partition : a number to partition around
109
+ :param pivot : a number to partition around
110
110
:return:
111
111
"""
112
- if ll .size == 1 :
113
- return True
114
112
# counter for keeping track of how many times
115
- # we change from < partition to >= partition
116
- partition_change_count = 0
117
- changed = False
113
+ # we change from < pivot to >= pivot
114
+ pivot_change_count = 0
118
115
n = ll .head
119
116
while n is not None :
120
- if n .data < partition and changed :
121
- changed = False
122
- partition_change_count += 1
123
- elif n .data >= partition and not changed :
124
- changed = True
125
- partition_change_count += 1
117
+ if n .data >= pivot :
118
+ pivot_change_count += 1
119
+ break
126
120
n = n .next
127
- return partition_change_count <= 1
121
+ while n is not None :
122
+ if n .data < pivot :
123
+ pivot_change_count += 1
124
+ break
125
+ n = n .next
126
+ return pivot_change_count <= 1
128
127
129
128
130
- def partition_ll (ll : LinkedList , partition : int ) -> LinkedList :
129
+ def partition_ll (ll : LinkedList , pivot : int ) -> LinkedList :
131
130
"""
132
131
This function will take in a linked list, a
133
- partition value, and will partition a linked
132
+ pivot value, and will partition a linked
134
133
list around a value x such that all nodes
135
134
less than x come before all nodes greater than
136
135
or equal to x. If x is in the list, then
@@ -140,15 +139,15 @@ def partition_ll(ll: LinkedList, partition: int) -> LinkedList:
140
139
Runtime: O(n)
141
140
Space Complexity: O(n)
142
141
:param ll: an input linked list
143
- :param partition : a number to partition around
142
+ :param pivot : a number to partition around
144
143
:return: a linked list that is 'partitioned'
145
144
"""
146
- left_partition = [] # will contain values < partition
147
- right_partition = [] # will contain values >= partition
145
+ left_partition = [] # will contain values < pivot
146
+ right_partition = [] # will contain values >= pivot
148
147
149
148
n = ll .head
150
149
while n is not None :
151
- if n .data < partition :
150
+ if n .data < pivot :
152
151
left_partition .append (n .data )
153
152
else :
154
153
right_partition .append (n .data )
@@ -228,13 +227,13 @@ def setUp(self):
228
227
]
229
228
230
229
def test_check_partitioned (self ):
231
- for ll , partition , output in self .check_partitioned_test_cases :
232
- self .assertEqual (check_partitioned (ll , partition ), output , msg = (ll , partition ))
230
+ for ll , pivot , output in self .check_partitioned_test_cases :
231
+ self .assertEqual (check_partitioned (ll , pivot ), output , msg = (ll , pivot ))
233
232
234
233
def test_partition (self ):
235
- for ll , partition in self .test_cases :
236
- partitioned_ll = partition_ll (ll , partition )
237
- self .assertTrue (check_partitioned (partitioned_ll , partition ), msg = (ll , partition ))
234
+ for ll , pivot in self .test_cases :
235
+ partitioned_ll = partition_ll (ll , pivot )
236
+ self .assertTrue (check_partitioned (partitioned_ll , pivot ), msg = (ll , pivot ))
238
237
239
238
240
239
if __name__ == '__main__' :
0 commit comments