forked from lanqiao-courses/python-100
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path013-partition_data.py
More file actions
30 lines (26 loc) · 847 Bytes
/
013-partition_data.py
File metadata and controls
30 lines (26 loc) · 847 Bytes
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
from linked_list import LinkedList
class MyLinkedList(LinkedList):
def partition(self, data):
if self.head is None:
return
left = MyLinkedList(None)
right = MyLinkedList(None)
curr = self.head
# Build the left and right lists
while curr is not None:
if curr.data < data:
left.append(curr.data)
elif curr.data == data:
right.insert_to_front(curr.data)
else:
right.append(curr.data)
curr = curr.next
curr_left = left.head
if curr_left is None:
return right
else:
# Merge the two lists
while curr_left.next is not None:
curr_left = curr_left.next
curr_left.next = right.head
return left