File tree Expand file tree Collapse file tree 1 file changed +28
-14
lines changed
Python/chapter02/2.4 - Partition Expand file tree Collapse file tree 1 file changed +28
-14
lines changed Original file line number Diff line number Diff line change @@ -83,21 +83,36 @@ def remove_node(self,data):
83
83
84
84
def partition (self ,num ):
85
85
#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
91
89
# Idea: Make two seperate llink and smash them together
92
90
while curr is not None :
93
91
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
+
101
116
102
117
103
118
@@ -118,5 +133,4 @@ def partition(self,num):
118
133
llist .printList ()
119
134
llist .lenght ()
120
135
llist .partition (5 )
121
-
122
- llist .lenght ()
136
+ llist .printList ()
You can’t perform that action at this time.
0 commit comments