The “Hello world” of Data Structure-Linked List.

Parag Naik
4 min readMay 12, 2021

Linked is type of linear data structure which is widely use to solve specific problem raise due to the limitation of arrays.
Linked list can be define as linear data structure in which the elements are not stored in contiguous memory location. In Laymen terms we can say a linked list is a collection of nodes in which each node has a data and the reference to the next node.

Diagram of a Linked List.

The first node of the linked list is called as Head and the last node of the linked list points to null. In this tutorial we will perfomer following operations:-
1.Insert at the start of Linked List.
2.Insert at the end of the Linked List.
3.Insert at a specific index /position in the Linked List.
I have implemented the above operation on linked list in python and using OOPs approach.

In python we have use two different class to represent node and head of the Linked List.

class Node:
# Function to initialize the node object
def __init__(self,data=None,next=None):
self.data=data #Assign data
self.next=next #Initialize next to Null

class Linkedlist:
# Function to initialize the Linked list object
def __init__(self):
self.head=None
  1. Insert element at the Start of the Linked List:-
    Inserting an element in linked list at start is comparatively easy.Here we have to create a node and make it as the head of the linked list and point the reference link of this node to next reference link of the head.Below is the code for it.
def insert_at_begining(self,data):
node=Node(data,self.head)
self.head=node

Here we will first create a node and pass data to it and reference link to this node will be that of head. Then we will this node as head.

2. Insert element at the end of Linked List:-
To insert the element at the end of the linked list we will have traverse the entire Linked-list.First we will have to check if the linked list is element or not ,for that we will check if the head is present or not.If not we will create a node and make it as head.If the head is present in Linked list,we will create a temperory variable say “itr” and give head as its value.Then we will iterate the entire list till we reach the end thats when the while loop will exit and then we will create a node with the next reference value as null.

def insert_at_end(self,data):
if self.head is None:
print("Linkedlist is empty")
node=Node(data,self.head)
self.head=node
itr=self.head
while itr.next:
itr=itr.next
itr.next=Node(data,None)

3.Insert at a specific index /position in the Linked List-
In this operation we have insert a node at a specific position of the Linked List.
a. first we will check if the given index is valid or not i.e it is in the range of length of linked list ,if not we will then raise an exception saying “Invalid Linked List”.
b. If the given position is index=0,then we will create a node and make it as head, pretty simple.
c. Now the tricky part of insert at other positions,first we will create a temperory variable “itr” which will hold the head of the linked list and a counter variable initialized to 0.Then we will iterate the entire linked list and check if we have reached at the previous node of given index.if so we will then create a node and point the reference link of the new node to the reference link of the previous node and point the previous node to the new node.If we are not the require index we will keep on iterating the linked list and also the counter.

def insert_at(self,index,data):
if index<0 or index>=self.get_length():
raise Exception("Invalide index given")

if index==0:
node=Node(data,self.head)
self.head=node
print("data inserted at begining")

itr=self.head
count=0
while itr.next:
if count==index-1:
node=Node(data,itr.next)
itr.next=node
itr=itr.next
count+=1

return "Node inserted"

Conclusion:-
Po from Kung-fu panda has taught we a lot and will keep on teaching me, that no matter how bad thing came/happened in past there is not point in holding onto it because it does not matter, its just either you can learn from it or run from it.I choose to learn from it.

Keep learning and keep hustling…(cheers:-PN)

--

--