-
Notifications
You must be signed in to change notification settings - Fork 3
/
01_singly_linked_list.py
230 lines (173 loc) · 4.93 KB
/
01_singly_linked_list.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
226
227
228
229
230
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def prepend(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
cur_node = self.head
self.head = new_node
new_node.next = cur_node
def insert_after(self, key, data):
cur = self.head
while cur:
if cur.next is None and cur.data == key:
self.append(data)
return
elif cur.data == key:
new_node = Node(data)
nxt = cur.next
new_node.next = nxt
cur.next = new_node
cur = cur.next
if cur is None:
print("Previous Node is not present in the list")
return
def delete_node(self, key):
# Case 1
cur_node = self.head
if cur_node and cur_node.data == key:
self.head = cur_node.next
cur_node = None
return
# Case 2
prev = None
while cur_node and cur_node.data != key:
prev = cur_node
cur_node = cur_node.next
if cur_node is None:
print("The Node is not present in the list")
return
prev.next = cur_node.next
cur_node = None
def delete_node_at_pos(self, pos):
cur_node = self.head
if pos == 0:
self.head = cur_node.next
cur_node = None
return
prev = None
count = 0
while cur_node and count != pos:
prev = cur_node
count += 1
cur_node = cur_node.next
if cur_node is None:
print("The Node is not present in the list")
return
prev.next = cur_node.next
cur_node = None
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def len_iterative(self):
count = 0
cur_node = self.head
while cur_node:
count += 1
cur_node = cur_node.next
return count
def len_recursive(self, node):
if node is None:
return 0
return 1 + self.len_recursive(node.next)
def swap_nodes(self, key_1, key_2):
if key_1 == key_2:
return
prev_1 = None
curr_1 = self.head
while curr_1 and curr_1.data != key_1:
prev_1 = curr_1
curr_1 = curr_1.next
prev_2 = None
curr_2 = self.head
while curr_2 and curr_2.data != key_2:
prev_2 = curr_2
curr_2 = curr_2.next
if not curr_1 and curr_2:
print("No Nodes found")
return
if prev_1:
prev_1.next = curr_2
else:
self.head = curr_2
if prev_2:
prev_2.next = curr_1
else:
self.head = curr_1
curr_1.next, curr_2.next = curr_2.next, curr_1.next
def print_holder(self, node, name):
if node is None:
print(name + " : None ")
else:
print(name + " : " + node.data)
def reverse_iterative(self):
prev = None
cur = self.head
while cur:
nxt = cur.next
cur.next = prev
self.print_holder(prev, "PREV")
self.print_holder(cur, "CUR")
self.print_holder(nxt, "NXT")
print("\n")
prev = cur
cur = nxt
self.head = prev
def reverse_recursive(self):
def _reverse_recursive(cur, prev):
if not cur:
return prev
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
return _reverse_recursive(cur, prev)
self.head = _reverse_recursive(cur = self.head, prev = None)
llist = SinglyLinkedList()
llist.append("A")
llist.append("B")
llist.append("C")
llist.append("D")
llist.insert_after("D", "S")
print(llist.len_iterative())
llist.prepend("E")
llist.delete_node("A")
llist.delete_node_at_pos(1)
llist.print_list()
print(llist.len_recursive(llist.head))
print("\n\n")
swappingNode = SinglyLinkedList()
swappingNode.append("A")
swappingNode.append("B")
swappingNode.append("C")
swappingNode.append("D")
swappingNode.swap_nodes("A", "D")
swappingNode.print_list()
print("\n\n")
llist3 = SinglyLinkedList()
llist3.append("A")
llist3.append("B")
llist3.append("C")
llist3.append("D")
llist3.reverse_iterative()
llist3.print_list()
print("\n")
llist3.reverse_recursive()
llist3.print_list()