forked from dsrao711/DSA-Together-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_insertion.py
62 lines (52 loc) · 1.51 KB
/
linked_list_insertion.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
class Node :
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.start = None
def InsertAtEnd(self , value):
newNode = Node(value)
if self.start == None:
self.start = newNode
return
else :
temp = self.start
while(temp.next != None):
temp = temp.next
temp.next = newNode
def Insert(self , position , value):
newNode = Node(value)
if position < 1 :
print("Position must be greater than 0")
#Insertion at beginning
elif position == 1 :
newNode.next = self.start
self.start = newNode
else :
temp = self.start
for i in range(0 , position - 1) :
if(temp.next != None):
temp = temp.next
if (temp != None):
newNode.next = temp.next
temp.next = newNode
else:
print("\n Previous node was null")
# Display the content of the list
def PrintList(self):
temp = self.start
if (temp != None):
while(temp != None):
print(temp.data)
temp = temp.next
else :
print("Linked list is empty")
MyList = LinkedList()
MyList.InsertAtEnd(10)
MyList.InsertAtEnd(20)
MyList.InsertAtEnd(30)
MyList.InsertAtEnd(40)
MyList.PrintList()
MyList.Insert(2,100)
MyList.PrintList()