Wednesday, February 8, 2023
HomeSoftware DevelopmentInsert a Node after a given Node in Linked Checklist

Insert a Node after a given Node in Linked Checklist


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Like arrays, Linked Checklist is a linear knowledge construction. In contrast to arrays, linked checklist components usually are not saved at a contiguous location; the weather are linked utilizing pointers. They embrace a sequence of related nodes. Right here, every node shops the info and the deal with of the following node.

To be taught extra about linked checklist check with the article “Introduction to Linked LIst“.

Given a linked checklist, the duty is to insert a brand new node after a given node of the linked checklist.

Insert new node after a given node in linked list

Insert new node after a given node in linked checklist

Instance:

Checklist = 1->2->4->5, Insert a node with worth 3 after the node with worth 2.
Output checklist shall be: 1->2->3->4->5

Contemplate the next representations of the linked checklist.

C++

class Node {

public:

    int knowledge;

    Node* subsequent;

};

C

struct Node {

    int knowledge;

    struct Node* subsequent;

};

Java

class LinkedList {

    Node head;

  

    

    class Node {

        int knowledge;

        Node subsequent;

  

        

        Node(int d)

        {

            knowledge = d;

            subsequent = null;

        }

    }

}

Python3

class Node:

  

    

    def __init__(self, knowledge):

        self.knowledge = knowledge 

        self.subsequent = None 

  

  

  

class LinkedList:

  

    

    def __init__(self):

        self.head = None

C#

public class Node {

    public int knowledge;

    public Node subsequent;

    public Node(int d)

    {

        knowledge = d;

        subsequent = null;

    }

}

Javascript

<script>

    var head;

  

    

    class Node {

  

        

        constructor(d) {

            this.knowledge = d;

            this.subsequent = null;

        }

    }

</script>

Strategy: Observe the beneath steps for inserting a node after a given node:

  • Firstly, verify 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 following of temp as the following of the earlier node. 
  • Lastly, transfer the following of the earlier node to level to temp.

Observe the beneath picture for a greater understanding.

How to insert a new node after given node in linked list

insert a brand new node after given node in linked checklist

Under is the implementation of the method.

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->knowledge = 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->knowledge = 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>

  

operate 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 House: O(1)



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments