You are on page 1of 12

Single Linked List-Insert

Insertion of a Node

at the beginning at the middle of


of the list the list

at the end of the


list
Steps to Insertion at the beginning of SLL

1. Create a new node, say newNode points to


the newly created node.
Steps to Insertion at the beginning

2. Link the newly created node with the head


node, i.e. the newNode will now point
to head node.
Steps to Insertion at the beginning

3. Make the new node as the head node, i.e.


now head node will point to newNode.
Steps to Insert node at the end of SLL

1. Create a new node and make sure that the


address part of the new node points
to NULL i.e.  newNode->next=NULL
Steps to Insert node at the end of SLL
2. Traverse to the last node of the linked list and
connect the last node of the list with the new
node, i.e. last node will now point to new
node. (lastNode->next = newNode).
Steps to Insert node at the middle of SLL

1. Create a new node.


Steps to Insert node at the middle of SLL

2. Traverse to the n-1th position of the linked list and connect the


new node with the n+1th node. Means the new node should
also point to the same node that the n-1th node is pointing to.
(newNode->next = temp->next where temp is the n-1th node).
Steps to Insert node at the middle of SLL

3. Now at last connect the n-1th node with the new node


i.e. the n-1th node will now point to new node. (temp-
>next = newNode where temp is the n-1th node).
PRACTICE SESSION
1. Write a program in C to insert a new node at
the beginning of a Singly Linked List and
display it in reverse order.
2. Write a program in C to insert a new node at
the end of a Singly Linked List and count the
number of nodes.
3. Write a program in C to insert a new node at
the middle of Singly Linked List.
THANK YOU

You might also like