Skip to main content

Singly Linked List with Python (Data Structure)

Singly Linked List : So In this post we're gonna talk about implementation of singly linked list using Python and also implementation of some basic operation on linked list...

  • For Creating Node:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
  •  From Here Linked List and Operation on linked list:
class LinkedList:
def __init__(self):
self.head = None
  • Insertion on Node in Linked list: 

   - Insert At End:

def insertAtEnd(self, newdata):
NewNode = Node(newdata) # creating Node
# if linked list is empty
if self.head is None:
self.head = NewNode
return
# if not empty
cur = self.head
# to get last element's next
while cur.next:
cur = cur.next
# set last element as NewNode
cur.next = NewNode
print(newdata, " Added At the End of Linked List")

   - Insert At Beginning:

def insertAtBeginning(self,newdata):
NewNode = Node(newdata)
# Every NewNode's next will be None
# update head value of linked list
NewNode.next = self.head
self.head = NewNode
print(newdata," Added At the beginning of Linked List")

   - Insert In Between:

def insertInBetween(self,middle_node,newdata):
# newdata will be added after middle_node(first occurence of 
        middle_node.data)
if middle_node is None:
print("Middle Node is None....")
return
NewNode = Node(newdata)
NewNode.next = middle_node.next
middle_node.next = NewNode
print(newdata," Added In between ",middle_node.data," and ",
        NewNode.next.data)
  •  Traversing Linked List:
 
def traverse(self,optional=None):
cur = self.head
if cur is None:
print("Linked List is Empty")
return
print("Displaying elements of Linked List")
while cur is not None:
print(cur.data, end = ' ')
cur = cur.next
print()
 
  • Removing Data:
# it removes first occurrence of removedata in linked list
def removeNode(self,removedata):
cur = self.head
if cur == None:
print("Linked List is Empty")
return
if cur is not None:
if cur.data == removedata:
self.head = cur.next
cur = None
print(removedata," is removed from Linked List")
return
# ex: 1 2 3 4 5 6 where removedata = 3
prev = None
while cur:
if cur.data == removedata:
#prev.next.data = 3 replace by cur.next.data = 4
prev.next = cur.next
cur = None
print(removedata," is removed from Linked List")
return
prev = cur #cur.data = 2
cur = cur.next #cur.data = 3
print(removedata," is not in Linked List")
    
  • Search Data:
def search(self, searchdata):
cur = self.head
while cur:
if cur.data == searchdata:
print(searchdata," is in Linked List")
return
cur = cur.next
print(searchdata," is not availabel in Linked List")

  • Update First Occurrence:
def update(self, olddata, newdata):
cur = self.head
while cur:
if cur.data == olddata:
cur.data = newdata
print(newdata," replaced by ",olddata)
return
cur = cur.next
print(olddata," is not availabel in Linked List")

  • Merge Two Linked List:
def mergeLinkedList(self,head1,head2):
if head1 is None:
return head2
if head2 is None:
return head1
while head1.next:
head1 = head1.next
head1.next = head2
 
 
Note: If there is some indentation problem while pasting code so take a look at          that problem , and If you have any better implementation then you can          comment.
      Open for suggestion

Next Post will be about sorting Algorithms of singly Linked List in Python

Thank You 😊😊
 
Sorting Algorithms:

Comments

Popular posts from this blog

Products Which I like

headphone Buy now Camera Buy now

What is Docker?

 Hello Everyone , Today we'll talk about what is docker ?, how to use it ? and why you should use it ?, then let's begin...

What is Digital Root ?

 What is a Digital root? if you have done little competitive programming or practice for Data Structure or anything else you have got a problem where you need to sum all digits of the number and do this process until the number becomes a single-digit number. so that is Digital Root, let's talk about it.