-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsll.py
More file actions
199 lines (153 loc) · 4.18 KB
/
sll.py
File metadata and controls
199 lines (153 loc) · 4.18 KB
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
'''
Nodes:
data, next
data -> 'A'
next -> Node that holds the data element 'B' (or any Node that comes after)
A -> B -> C -> D -> None
'''
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
# based around the concept of Nodes
# creates a chain of Nodes by connecting them with one another
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
# handle for if the list is empty
if not self.head:
self.head = new_node
return
# handle for if the list is not empty
cur_node = self.head
while cur_node.next:
cur_node = cur_node.next
cur_node.next = new_node
def print_list(self):
# A -> B -> C -> None
# self.head = "A" -> self.head -> "B" -> self.head -> "C"... -> self.head -> None
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def prepend(self, data):
# add a node at the very start
new_node = Node(data)
# handle if the list is empty
if not self.head:
self.head = new_node
return
# handle if the list is not empty
old_head = self.head
self.head = new_node
self.head.next = old_head
def insert(self, prev_node, data):
# A -> B -> C -> D -> E -> None
# A -> B -> new -> C -> D -> E -> None
new_node = Node(data)
cur = self.head
while cur and cur != prev_node:
cur = cur.next
if not cur:
return
nxt = cur.next
cur.next = new_node
new_node.next = nxt
def reverse(self):
"""
None 1 (head) -> 2 -> 3 -> 4 -> None
None <- 1 <- 2 <- 3 <- 4 (head)
"""
prev = None
cur = self.head
while cur:
next_node = cur.next
cur.next = prev
prev = cur
cur = next_node
self.head = prev
def delete(self, data):
# handle if the list is empty
if not self.head:
print('List is empty.')
return
prev = None
cur = self.head
while cur:
# handles for if the current node is a node to delete
if cur.data == data:
# handles if it is the head node
if cur == self.head:
self.head = self.head.next
cur = self.head # B
else:
# handles if it is a node in the middle
nxt = cur.next
prev.next = nxt
del cur
cur = nxt
# node is not a node to delete
else:
prev = cur
cur = cur.next
# how to find the length of a sll
def __len__(self):
count = 0
cur = self.head
# A > B > C > D > None
while cur:
count += 1
cur = cur.next
return count
def merge(self, sll2):
if not self.head:
return sll2.head
if not sll2.head:
return self.head
l1 = self.head
l2 = sll2.head
new_list = None
if l1.data <= l2.data:
new_list = l1
l1 = l1.next
elif l2.data < l1.data:
new_list = l2
l2 = l2.next
while l1 and l2:
if l1.data <= l2.data:
new_list.next = l1
l1 = l1.next
new_list = new_list.next
elif l2.data < l1.data:
new_list.next = l2
l2 = l2.next
new_list = new_list.next
if not l2:
new_list.next = l1
if not l1:
new_list.next = l2
return new_list
"""
sll = SinglyLinkedList()
sll.append(1)
sll.append(3)
sll.append(5)
sll2 = SinglyLinkedList()
sll2.append(2)
sll2.append(4)
sll.merge(sll2)
sll.print_list()
"""
"""
sll = SinglyLinkedList()
sll.append('A')
sll.append('B')
sll.append('C')
sll.append('D')
sll.append('E')
sll.print_list()
print("\n")
sll.reverse()
"""