Delete node
You have been given a linked list of integers. Your task is to write a function that deletes a node from a given position, 'pos'.
Note :
Illustration :
Image-I :
Image-II :
Input format :
Remember/Consider :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedListNode: | |
def __init__(self,data): | |
self.data=data | |
self.next=None | |
class LinkedList: | |
def __init__(self): | |
self.head = None | |
self.tail=None | |
def inputLL(self,arr): | |
if len(arr)==0: | |
return | |
for i in arr: | |
if i is -1: | |
return | |
NewNode=LinkedListNode(i) | |
if self.head is None: | |
self.head=NewNode | |
self.tail=NewNode | |
else: | |
self.tail.next=NewNode | |
self.tail=NewNode | |
def deleteLL(self,data): | |
temp=self.head | |
if temp.data==data: | |
self.head=temp.next | |
temp=None | |
return | |
while temp is not None: | |
if temp.data==data: | |
break | |
prev=temp | |
temp=temp.next | |
prev.next=temp.next | |
temp=None | |
def printLL(self): | |
while self.head is not None: | |
print(self.head.data,end=" ") | |
self.head=self.head.next | |
print() | |
arr=list(map(int,input().split())) #Example: 1,2,3,4,5 | |
l=LinkedList() | |
l.inputLL(arr) | |
l.deleteLL(2) | |
l.printLL() | |
Comments
Post a Comment
Please give us your valuable feedback