Insert a Node after a given Node in Linked List
Last Updated :
21 Aug, 2023
Given a linked list, the task is to insert a new node after a given node of the linked list.
Insert a new node after a given node in the linked list
Example:
Input: LinkedList = 2->3->4->5, NewNode = 1, InsertAfter = 2
Output: LinkedList = 2->1->3->4->5
Input: LinkedList = , NewNode = 1, InsertAfter = 2
Output: No such node found
Approach:
To insert a node after a given node in a Linked List, we need to:
- Check if the given node exists or not.
- If it do not exists,
- If the given node exists,
- Make the element to be inserted as a new node
- Change the next pointer of given node to the new node
- Now shift the original next pointer of given node to the next pointer of new node
Follow the below steps for inserting a node after a given node:
- Firstly, check if the given previous node is NULL or not.
- Then, allocate a new node (say temp) and
- Assign the data to temp.
- And then make the next of temp as the next of the previous node.
- Finally, move the next of the previous node to point to temp.
Below is the implementation of the approach.
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void insertAfter(Node* prev_node, int new_data)
{
if (prev_node == NULL) {
cout << "The given previous node cannot be NULL" ;
return ;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* node)
{
while (node != NULL) {
cout << " " << node->data;
node = node->next;
}
cout << "\n" ;
}
int main()
{
Node* head = NULL;
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
cout << "Created Linked list is: " ;
printList(head);
insertAfter(head, 1);
cout << "After inserting 1 after 2: " ;
printList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAfter( struct Node* prev_node, int new_data)
{
if (prev_node == NULL) {
printf ( "The given previous node cannot be NULL" );
return ;
}
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* node)
{
while (node != NULL) {
printf ( " %d" , node->data);
node = node->next;
}
printf ( "\n" );
}
int main()
{
struct Node* head = NULL;
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
printf ( "Created Linked list is: " );
printList(head);
insertAfter(head, 1);
printf ( "After inserting 1 after 2: " );
printList(head);
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node next;
}
class LinkedList {
Node head;
void insertAfter(Node prev_node, int new_data)
{
if (prev_node == null ) {
System.out.println(
"The given previous node cannot be NULL" );
return ;
}
Node new_node = new Node();
new_node.data = new_data;
new_node.next = prev_node.next;
prev_node.next = new_node;
}
void push( int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head;
head = new_node;
}
void printList()
{
Node node = head;
while (node != null ) {
System.out.print( " " + node.data);
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push( 6 );
llist.push( 5 );
llist.push( 4 );
llist.push( 3 );
llist.push( 2 );
System.out.print( "Created Linked list is: " );
llist.printList();
llist.insertAfter(llist.head, 1 );
System.out.print( "After inserting 1 after 2: " );
llist.printList();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def insertAfter( self , prev_node, new_data):
if prev_node is None :
print ( "The given previous node must inLinkedList." )
return
new_node = Node(new_data)
new_node. next = prev_node. next
prev_node. next = new_node
def printList( self ):
temp = self .head
while temp:
print (temp.data, end = " " )
temp = temp. next
if __name__ = = "__main__" :
llist = LinkedList()
llist.push( 6 )
llist.push( 5 )
llist.push( 4 )
llist.push( 3 )
llist.push( 2 )
print ( "Created linked list is: " )
llist.printList()
llist.insertAfter(llist.head, 1 )
print ( "\nAfter inserting 1 after 2: " )
llist.printList()
|
C#
using System;
class GFG {
public Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void insertAfter(Node prev_node, int new_data)
{
if (prev_node == null ) {
Console.WriteLine( "The given previous"
+ " node cannot be null" );
return ;
}
Node new_node = new Node(new_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
public void printList()
{
Node tnode = head;
while (tnode != null ) {
Console.Write(tnode.data + " " );
tnode = tnode.next;
}
}
public static void Main(String[] args)
{
GFG llist = new GFG();
llist.push(6);
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
Console.Write( "Created Linked list is: " );
llist.printList();
llist.insertAfter(llist.head, 1);
Console.Write( "\nAfter inserting 1 after 2: " );
llist.printList();
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
class LinkedList
{
constructor()
{
this .head = null ;
}
insertAfter(prev_node, new_data)
{
if (!prev_node)
{
console.log( "The given previous node cannot be NULL" );
return ;
}
const new_node = new Node(new_data);
new_node.next = prev_node.next;
prev_node.next = new_node;
}
push(new_data) {
const new_node = new Node(new_data);
new_node.next = this .head;
this .head = new_node;
}
printList()
{
let node = this .head;
const result = [];
while (node !== null ) {
result.push(node.data);
node = node.next;
}
console.log(result.join( " " ));
}
}
const llist = new LinkedList();
llist.push(6);
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
console.log( "Created Linked list is:" );
llist.printList();
llist.insertAfter(llist.head, 1);
console.log( "After inserting 1 after 2:" );
llist.printList();
|
Output
Created Linked list is: 2 3 4 5 6
After inserting 1 after 2: 2 1 3 4 5 6
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...