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

Makefile

 You may have seen file named Makefile or you may have type command like make, make install. so what is this file Makefile and what are this command. let's discuss about it.

Products Which I like

Induction Buy now Induction Buy now

Pascal's Triangle

 Pascal's Triangle Introduction In mathematics, Pascal’s triangle is triangular array of binomial coefficient. below you can see how it looks like… 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 How to Build it? First of all start with “1” and second rows as “1 1” then we can clearly see every element in next rows(except 1) if sum of two elements from above row. (i.e, 1+1=2, 1+2=3, 1+3=4) and with this rules we can create pascal’s triangle of n rows. Below is visual of how it will look like, Formula let’s take 0th row as first row and 0th column as first column, so we can get each value using formula where n is row number and k is column number. so for finding 1st(0 indexed) element in 2nd row we can write 2C1 which will give 2. There is one another technique, in which we need to solve (x + y)^n for nth row, if we solve (x +y)² then we will get 2nd row as coefficient of x and y in this solved formula which is x² + 2xy + y² . coefficients are (...