Insert a Node at Entrance/Starting of a Linked Listing

[ad_1]

View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Like arrays, Linked Listing is a linear knowledge construction. Not like arrays, linked checklist parts usually are not saved at a contiguous location; the weather are linked utilizing pointers. They embody a sequence of related nodes. Right here, every node shops the info and the handle of the following node.

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

Given a linked checklist, the duty is to insert a brand new node at the start of the linked checklist.

Insert Node at the front of a linked list

Insert Node on the entrance of a linked checklist

Instance:

Listing = 2->3->4->5, Insert a node with worth 1 at starting.
Output checklist can 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: The beneath steps must be adopted to insert a brand new node on the entrance of the linked checklist

  • Allocate a brand new node (say temp).
  • Put the required knowledge into temp.
  • The ‘subsequent’ pointer of the node must be pointed to the present head.
  • Now make the head pointer level to temp.

See the beneath picture for a greater unedrstanding:

Insert a new node at front of Linked List

Insert a brand new node at entrance of Linked Listing

Beneath is the implementation of the method:

C++

void push(Node** head_ref, int new_data)

{

  

    

    Node* new_node = new Node();

  

    

    new_node->knowledge = new_data;

  

    

    new_node->subsequent = (*head_ref);

  

    

    

    (*head_ref) = new_node;

}

C

   

   

 

void push(struct Node** head_ref, int new_data)

{

    

    struct Node* new_node

        = (struct Node*)malloc(sizeof(struct Node));

  

    

    new_node->knowledge = new_data;

  

    

    new_node->subsequent = (*head_ref);

  

    

    (*head_ref) = new_node;

}

Java

   

   

public void push(int new_data)

{

    

              

    Node new_node = new Node(new_data);

  

    

    new_node.subsequent = head;

  

    

    head = new_node;

}

Python3

def push(self, new_data):

  

    

    

    new_node = Node(new_data)

  

    

    new_node.subsequent = self.head

  

    

    self.head = new_node

C#

public void push(int new_data)

{

    

               

    Node new_node = new Node(new_data);

  

    

    new_node.subsequent = head;

  

    

    head = new_node;

}

Javascript

<script>

  

   

   

     

 perform push(new_data)

{

    

              

    var new_node = new Node(new_data);

  

    

    new_node.subsequent = head;

  

    

    head = new_node;

}

  

  

</script>

Time Complexity: O(1)
Auxiliary House: O(1)

[ad_2]

Leave a Reply