Skip to content

Instantly share code, notes, and snippets.

@tiwarinaman
Created August 30, 2020 09:22
Show Gist options
  • Select an option

  • Save tiwarinaman/55396528952965a6ac40055f8c48b6aa to your computer and use it in GitHub Desktop.

Select an option

Save tiwarinaman/55396528952965a6ac40055f8c48b6aa to your computer and use it in GitHub Desktop.
Insertion and deletion in linkedlist
class node:
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.start = None
def insertLast(self,value):
new_node=node(value)
if self.start==None:
self.start = new_node
else:
temp = self.start
while temp.next !=None:
temp = temp.next
temp.next = new_node
def deleteFirst(self):
if self.start == None:
print('No element present')
else:
self.start = self.start.next
def viewList(self):
if self.start == None:
print('List is empty')
else:
temp = self.start
while temp !=None:
print(temp.data,end=' ')
temp = temp.next
obj =LinkedList()
obj.insertLast(10)
obj.insertLast(20)
obj.insertLast(30)
obj.insertLast(40)
obj.insertLast(50)
print()
obj.viewList()
print()
obj.deleteFirst()
obj.viewList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment