-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.py
45 lines (40 loc) · 992 Bytes
/
slist.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
class Node():
def __init__(self, val, next=None):
self.val = val
self.next = next
class SList():
def __init__(self, value):
self.head = Node(value)
def add(self, value):
ptr = self.head
while ptr.next:
ptr = ptr.next
ptr.next = Node(value)
return self
def remove(self, value):
current = self.head
previous = None
while current:
if current.val == value:
if previous:
previous.next = current.next
else:
self.head = None
return self
current = current.next
prev = current
return self
def display(self):
ptr = self.head
while ptr:
print('Value: ', ptr.val, 'Next:', ptr.next)
ptr = ptr.next
return self
s = SList(4)
s.add(10)
s.add(17)
s.remove(17)
s.remove(4)
s.remove(10)
s.remove(11)
s.display()