-
Notifications
You must be signed in to change notification settings - Fork 0
/
single link list.py
63 lines (56 loc) · 1.61 KB
/
single link 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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insertAtBeginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def insertAtEnd(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node
def insertAfter(self, prev_node, data):
if prev_node is None:
return
new_node = Node(data)
new_node.next = prev_node.next
prev_node.next = new_node
def deleteNode(self, position):
if self.head is None:
return
temp = self.head
if position == 0:
self.head = temp.next
temp = None
return
for _ in range(position - 1):
temp = temp.next
if temp is None or temp.next is None:
return
next = temp.next.next
temp.next = None
temp.next = next
def display(self):
temp = self.head
while temp:
print(temp.data, end=" -> ")
temp = temp.next
print("None")
if __name__ == "__main__":
llist = LinkedList()
llist.insertAtBeginning(10)
llist.insertAtEnd(20)
llist.insertAtEnd(30)
llist.insertAfter(llist.head.next, 25)
llist.display()
llist.deleteNode(2)
llist.display()