-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion and deletion.py
40 lines (36 loc) · 1.12 KB
/
insertion and deletion.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
def insert(self,newnode):
if self.head is None:
self.head=newnode
else:
lastnode=self.head
while(True):
if lastnode.next is None:
break
lastnode=lastnode.next
lastnode.next=newnode
def insertat(self,newnode,position):
if position == 0:
self.insertashead(newnode)
return
currentnode=self.head
#previousnode=self.head
currentpos=0
while True:
if(currentpos==position):
previousnode.next=newnode
newnode.next=currentnode
break
previousnode=currentnode
currentnode=currentnode.next
currentpos+=1
def insertashead(self,newnode):
tempnode=self.head
self.head=newnode
self.head.next=tempnode
del tempnode
def delend(self):
lastnode=self.head
while(lastnode.next is not None):
previousnode=lastnode
lastnode=lastnode.next
previousnode.next=None