-
Notifications
You must be signed in to change notification settings - Fork 0
/
listWorks.py
225 lines (186 loc) · 5.3 KB
/
listWorks.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
class Elem:
def __init__(self, value=None, nextElem=None):
self.value = value
self.nextElem = nextElem
def reverse(self, prevElem):
next = self.nextElem
self.nextElem = prevElem
if next != None:
return next.reverse(self)
else:
return self
def mergeRecur(self, another):
if another is None:
return self
if self.value <= another.value:
self.nextElem = another.mergeRecur(self.nextElem)
return self
else:
another.nextElem = self.mergeRecur(another.nextElem)
return another
def removeDups(self):
if self.nextElem is None:
return self
self.nextElem = self.nextElem.removeDups()
prev = None
curr = self
while curr is not None:
if curr.value == self.value:
prev.nextElem = curr.nextElem
prev = curr
curr = curr.nextElem
return self
class MyList:
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def setHead(self, head):
self.head = head
def add(self, val):
newElem = Elem(val)
if self.head == None:
self.head = newElem
self.tail = newElem
self.tail.nextElem = newElem
self.tail = newElem
self.length += 1
def print(self):
cursor = self.head
while cursor != None:
print(cursor.value)
cursor = cursor.nextElem
def reverse(self):
prevNode = None
nextNode = None
currentNode = self.head
while currentNode != None:
nextNode = currentNode.nextElem
currentNode.nextElem = prevNode
prevNode = currentNode
currentNode = nextNode
self.head = prevNode
def removeDuplicates(self):
self.head = self.head.removeDup()
def reverseRecursive(self, curr, next):
if next.nextElem == None:
return next
newHead = self.reverseRecursive(next, next.nextElem)
newHead.next = next
def splitAlternate(self):
if self.head.nextElem is None:
return (self, None)
firstSplitted = MyList()
secondSplitted = MyList()
curr = self.head
next = curr.nextElem
firstSplitted.setHead(curr)
secondSplitted.setHead(next)
while next is not None:
curr.nextElem = next.nextElem
curr = next
next = curr.nextElem
return (firstSplitted, secondSplitted)
def rearrange(self):
slow = self.head
fast = self.head
while fast != self.tail:
slow = slow.nextElem
fast = fast.nextElem
if fast != None:
fast = fast.nextElem
# now slow is at mid, fast at end
frontCursor = self.head
while frontCursor != slow:
endCursor = slow
endCursorPrev = endCursor
while endCursor.nextElem != None:
endCursorPrev = endCursor
endCursor = endCursor.nextElem
temp = frontCursor.nextElem
frontCursor.nextElem = endCursor
endCursor.nextElem = temp
endCursorPrev.nextElem = None
frontCursor = temp
self.tail = slow
def mergeRecur(self, another):
merged = self.head.mergeRecur(another.head)
self.head = merged
def merge(self, another):
# list 1 is 1,3,5,7
# list 2 is 2,4,6,8
c1 = self.head
c2 = another.head
prev1 = None
while c1 != None and c2 != None:
if c1.value < c2.value:
prev1 = c1
c1 = c1.nextElem
else:
prev1.nextElem = c2
temp = c2.nextElem
c2.nextElem = c1
c1 = c2
c2 = temp
if c2 != None:
prev1.nextElem = c2
self.length += another.length
def swapForK(self, k):
self.head = self.swapRecursively(self.head, k)
def swapRecursively(self, begin, k):
if begin == None:
return begin
if k == 0:
return begin
begin.nextElem = self.swapRecursively(begin.nextElement, k-1)
return begin
def swapIteratively(self, begin, k):
i = 0
curr = begin
prev = None
while i < k and curr is not None:
next = curr.nextElem
curr.nextElem = prev
prev = curr
curr = next
i += 1
return prev
def reverseR(self):
self.head = self.head.reverse(None)
myList = MyList()
myList.add(1)
myList.add(2)
myList.add(3)
myList.add(4)
myList.add(5)
myList.reverseR()
myList.print()
print("Merging 2 sorted lists")
myList = MyList()
myList.add(1)
myList.add(4)
myList.add(7)
myList.add(11)
secondList = MyList()
secondList.add(2)
secondList.add(3)
secondList.add(9)
secondList.add(17)
myList.mergeRecur(secondList)
myList.print()
print("Swapping every 4 nodes")
myList = MyList()
myList.add(1)
myList.add(2)
myList.add(3)
myList.add(4)
myList.add(5)
myList.add(6)
myList.add(7)
myList.add(8)
myList.add(9)
myList.add(10)
#myList.swapForK(4)
splitted = myList.splitAlternate()
splitted[0].print()
splitted[1].print()