Insertion

Insertion in a Linked List

Inserting element in the linked list involves re-assigning the pointers from the existing nodes to the newly inserted node. Depending on whether the new data element is getting inserted at the beginning or at the middle or at the end of the linked list, we have the below scenarios.

Inserting at the Beginning of the Linked List

This involves pointing the next pointer of the new data node to the current head of the linked list. So the current head of the linked list becomes the second data element and the new node becomes the head of the linked list.

class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None
class SLinkedList:
    def __init__(self):
        self.headval = None

# Print the linked list
    def listprint(self):
        printval = self.headval
        while printval is not None:
            print (printval.dataval)
            printval = printval.nextval
    def AtBegining(self,newdata):
        NewNode = Node(newdata)

 
# Update the new nodes next val to existing node
        NewNode.nextval = self.headval
        self.headval = NewNode

 
list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

 
list.headval.nextval = e2
e2.nextval = e3

 
list.AtBegining("Sun")

 
list.listprint()

When the above code is executed, it produces the following result:

Sun
Mon
Tue
Wed 

 Inserting at the End of the Linked List

This involves pointing the next pointer of the the current last node of the linked list to the new data node. So the current last node of the linked list becomes the second last data node and the new node becomes the last node of the linked list.

class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None

 
class SLinkedList:
    def __init__(self):
        self.headval = None

 
# Function to add newnode
    def AtEnd(self, newdata):
        NewNode = Node(newdata)
        if self.headval is None:
            self.headval = NewNode
            return
        laste = self.headval
        while(laste.nextval):
            laste = laste.nextval
        laste.nextval=NewNode

 
# Print the linked list
    def listprint(self):
        printval = self.headval
        while printval is not None:
            print (printval.dataval)
            printval = printval.nextval

 

 
list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

 
list.headval.nextval = e2
e2.nextval = e3

 
list.AtEnd("Thu")

 
list.listprint()

When the above code is executed, it produces the following result:

Mon
Tue
Wed
Thu

Inserting in between two Data Nodes

This involves chaging the pointer of a specific node to point to the new node. That is possible by passing in both the new node and the existing node after which the new node will be inserted. So we define an additional class which will change the next pointer of the new node to the next pointer of middle node. Then assign the new node to next pointer of the middle node.

class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None

 
class SLinkedList:
    def __init__(self):
        self.headval = None

 
# Function to add node
    def Inbetween(self,middle_node,newdata):
        if middle_node is None:
            print("The mentioned node is absent")
            return

 
        NewNode = Node(newdata)
        NewNode.nextval = middle_node.nextval
        middle_node.nextval = NewNode

 
# Print the linked list
    def listprint(self):
        printval = self.headval
        while printval is not None:
            print (printval.dataval)
            printval = printval.nextval

 

 
list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Thu")

 
list.headval.nextval = e2
e2.nextval = e3

 
list.Inbetween(list.headval.nextval,"Fri")

 
list.listprint()

 When the above code is executed, it produces the following result:

Mon
Tue
Fri
Thu

Try here>>>