Like arrays, Linked Listing is a linear information construction. Not like arrays, linked record parts usually are not saved at a contiguous location; the weather are linked utilizing pointers. They embody a collection of linked nodes. Right here, every node shops the info and the deal with of the subsequent node.
To study extra about linked record seek advice from the article “Introduction to Linked LIst“.
Given a linked record, the duty is to insert a brand new node after a given node of the linked record.
Insert new node after a given node in linked record
Instance:
Listing = 1->2->4->5, Insert a node with worth 3 after the node with worth 2.
Output record will likely be: 1->2->3->4->5
Think about the next representations of the linked record.
C++
class Node {
public :
    int information;
    Node* subsequent;
};
|
C
struct Node {
    int information;
    struct Node* subsequent;
};
|
Java
class LinkedList {
    Node head;
 Â
   Â
    class Node {
        int information;
        Node subsequent;
 Â
       Â
        Node( int d)
        {
            information = d;
            subsequent = null ;
        }
    }
}
|
Python3
class Node:
 Â
   Â
    def __init__( self , information):
        self .information = informationÂ
        self . subsequent = None Â
 Â
 Â
 Â
class LinkedList:
 Â
   Â
    def __init__( self ):
        self .head = None
|
C#
public class Node {
    public int information;
    public Node subsequent;
    public Node( int d)
    {
        information = d;
        subsequent = null ;
    }
}
|
Javascript
<script>
    var head;
 Â
   Â
    class Node {
 Â
       Â
        constructor(d) {
            this .information = d;
            this .subsequent = null ;
        }
    }
</script>
|
Strategy: Comply with the under steps for inserting a node after a given node:
- Firstly, test if the given earlier node is NULL or not.
- Then, allocate a brand new node (say temp) and
- Assign the info to temp.
- After which make the subsequent of temp as the subsequent of the earlier node.Â
- Lastly, transfer the subsequent of the earlier node to level to temp.
Comply with the under picture for a greater understanding.
The way to insert a brand new node after given node in linked record
Beneath is the implementation of the strategy.
C++
void insertAfter(Node* prev_node, int new_data)
{
   Â
    if (prev_node == NULL) {
        cout << "The given earlier node can't be NULL" ;
        return ;
    }
 Â
   Â
    Node* new_node = new Node();
 Â
   Â
    new_node->information = new_data;
 Â
   Â
   Â
    new_node->subsequent = prev_node->subsequent;
 Â
   Â
   Â
    prev_node->subsequent = new_node;
}
|
C
void insertAfter( struct Node* prev_node, int new_data)
{
   Â
    if (prev_node == NULL) {
        printf ( "the given earlier node can't be NULL" );
        return ;
    }
 Â
   Â
    struct Node* new_node
        = ( struct Node*) malloc ( sizeof ( struct Node));
 Â
   Â
    new_node->information = new_data;
 Â
   Â
    new_node->subsequent = prev_node->subsequent;
 Â
   Â
    prev_node->subsequent = new_node;
}
|
Java
public void insertAfter(Node prev_node, int new_data)
{
   Â
    if (prev_node == null ) {
        System.out.println(
            "The given earlier node can't be null" );
        return ;
    }
 Â
   Â
   Â
    Node new_node = new Node(new_data);
 Â
   Â
    new_node.subsequent = prev_node.subsequent;
 Â
   Â
    prev_node.subsequent = new_node;
}
|
Python3
def insertAfter( self , prev_node, new_data):
 Â
   Â
    if prev_node is None :
        print ( "The given earlier node should inLinkedList." )
        return
 Â
   Â
   Â
    new_node = Node(new_data)
 Â
   Â
    new_node. subsequent = prev_node. subsequent
 Â
   Â
    prev_node. subsequent = new_node
|
C#
public void insertAfter(Node prev_node, int new_data)
{
   Â
    if (prev_node == null ) {
        Console.WriteLine( "The given earlier node"
                          + " can't be null" );
        return ;
    }
 Â
   Â
           Â
    Node new_node = new Node(new_data);
 Â
   Â
               Â
    new_node.subsequent = prev_node.subsequent;
 Â
   Â
                   Â
    prev_node.subsequent = new_node;
}
|
Javascript
<script>
 Â
perform insertAfter(prev_node, new_data)Â
{Â
 Â
   Â
    if (prev_node == null )Â
    {Â
        doc.write( "The given earlier node can't be null" );Â
        return ;Â
    }Â
 Â
   Â
   Â
    var new_node = new Node(new_data);Â
 Â
   Â
    new_node.subsequent = prev_node.subsequent;Â
 Â
   Â
    prev_node.subsequent = new_node;Â
}
 Â
 Â
</script>
|
Time Complexity: O(1)
Auxiliary Area: O(1)