-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlylinkedlist.py
53 lines (42 loc) · 1.04 KB
/
singlylinkedlist.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
class Node(object):
def __init__(self, dat, nxt):
self.dat = dat
self.nxt = nxt
class SingleList(object):
def __init__(self):
self.head = None
self.tail = None
def show(self):
print "Printing the linked-list data"
current_node = self.head
while current_node is not None:
print current_node.dat, "->",
current_node = current_node.nxt
print None
def append(self, dat):
node = Node(dat, None)
if self.head is None:
self.head = self.tail = node
else:
self.tail.nxt = node
self.tail = node
def remove(self, node_val):
current_node = self.head
previous_node = None
while current_node is not None:
if current_node.dat == node_val:
if previous_node.nxt is not None:
previous_node.nxt = current_node.nxt
else:
self.head = current_node.nxt
previous_node = current_node
current_node = current_node.nxt
if __name__ == '__main__':
s = SingleList()
s.append(31)
s.append(2)
s.append(10)
s.append(3)
s.show()
s.remove(10)
s.show()