Skip to content

Commit cba19dd

Browse files
This might be a solution
1 parent a9fb30c commit cba19dd

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

Python/chapter02/2.4 - Partition/camilo_solution_2.4.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,36 @@ def remove_node(self,data):
8383

8484
def partition(self,num):
8585
#llist_A and llist_b to be continers
86-
llist_A = linklist()
87-
llist_B = linklist()
88-
head_A = llist_A.head
89-
head_B = llist_B.head
90-
curr= self.head
86+
list_A = linklist()
87+
list_B = linklist()
88+
curr = self.head
9189
# Idea: Make two seperate llink and smash them together
9290
while curr is not None:
9391
if curr.data <= num:
94-
llist_A.push(curr.data)
95-
else:
96-
llist_B.push(curr.data)
97-
while head_B is not None:
98-
llist_A.push(head_B.data)
99-
head_B = head_B.next
100-
llist_A.printList()
92+
list_A.push(curr.data)
93+
elif curr.data > num:
94+
list_B.push(curr.data)
95+
curr = curr.next
96+
list_A.printList()
97+
list_B.printList()
98+
99+
#Go through all of A the point the end of A to the begining of B
100+
self.head = list_A.head
101+
head_A = list_A.head
102+
while head_A:
103+
if head_A.next is not None:
104+
head_A = head_A.next
105+
106+
head_A.next = list_B.head
107+
head_A = head_A.next
108+
109+
110+
111+
112+
113+
114+
115+
101116

102117

103118

@@ -118,5 +133,4 @@ def partition(self,num):
118133
llist.printList()
119134
llist.lenght()
120135
llist.partition(5)
121-
122-
llist.lenght()
136+
llist.printList()

0 commit comments

Comments
 (0)