forked from sourav-122/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertnodelist.py
93 lines (43 loc) · 1.34 KB
/
insertnodelist.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
# Function to insert a new node at the beginning
def push(self, x):
new_node = Node(x)
new_node.next = self.head
self.head = new_node
def insertAfter(self, prev_node, x):
if prev_node is None:
print("The given previous node must inLinkedList.")
return
new_node = Node(x)
new_node.next = prev_node.next
prev_node.next = new_node
def append(self, x):
new_node = Node(x)
if self.head is None:
self.head = new_node
return
last = self.head
while (last.next):
last = last.next
last.next = new_node
def printList(self):
temp = self.head
while (temp):
print(temp.data,end=" ")
temp = temp.next
# Code execution starts here
if __name__=='__main__':
llist = LinkedList()
llist.append(6)
llist.push(7);
llist.push(1);
llist.append(4)
llist.insertAfter(llist.head.next, 8)
print('Created linked list is: ')
llist.printList()