Skip to content

Commit 6f54972

Browse files
committed
completed 2.4
1 parent 897a20a commit 6f54972

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

chapter-2-linked-lists/2.4-partition.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,36 @@
44
Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
55
"""
66

7+
from LL_implementation import LLNode, LinkedList
8+
9+
def partition(ll, x):
10+
partitioned = LinkedList()
11+
current = ll.head
12+
while current.next:
13+
if current.data >= x:
14+
partitioned.add(current.data)
15+
else:
16+
partitioned.insert_at_head(current.data)
17+
current = current.next
18+
if current.data >= x:
19+
partitioned.add(current.data)
20+
else:
21+
partitioned.insert_at_head(current.data)
22+
return partitioned
723

824

925

1026

1127
if __name__ == "__main__":
12-
print "nothing yet"
28+
ll = LinkedList()
29+
ll.add(3)
30+
ll.add(5)
31+
ll.add(8)
32+
ll.add(5)
33+
ll.add(10)
34+
ll.add(2)
35+
ll.add(1)
36+
37+
new_ll = partition(ll, 5)
38+
new_ll._print() #remember that x itself can be anywhere in the second half of the output ll
39+

0 commit comments

Comments
 (0)