Insert Node on the Finish of a Linked Record

[ad_1]

View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Like arrays, Linked Record is a linear information construction. Not like arrays, linked record components will not be saved at a contiguous location; the weather are linked utilizing pointers. They embrace a sequence of linked nodes. Right here, every node shops the info and the tackle of the subsequent node.

To be taught 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 on the finish of the linked record.

Insert a node at the end of a linked list

Insert a node on the finish of a linked record

Instance:

Record = 1->2->3->4, Insert a node with worth 5 on the finish.
Output record will probably 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>

Method: Following is the strategy so as to add a brand new node on the finish of the linked record:

  • Allocate reminiscence for a brand new node (say temp) and create a pointer (say final) which factors the top of the linked record.
  • Set the info to be entered in temp.
  • temp would be the final node. Therefore the subsequent pointer of temp will level to null.
  • If linked record is empty make temp because the head.
  • Traverse utilizing the final pointer until we attain the top node of the linked record.
  • Now, level the subsequent node of final to temp.

Observe the beneath picture for a greater understanding:

How to insert new node at the end of the linekd list

How one can insert new node on the finish of the linekd record

Beneath is the implementation of the strategy:

C++

void append(Node** head_ref, int new_data)

{

    

    Node* new_node = new Node();

  

    

    Node* final = *head_ref;

  

    

    new_node->information = new_data;

  

    

    

    

    new_node->subsequent = NULL;

  

    

    

    if (*head_ref == NULL) {

        *head_ref = new_node;

        return;

    }

  

    

    whereas (last->subsequent != NULL) {

        final = last->subsequent;

    }

  

    

    last->subsequent = new_node;

    return;

}

C

   

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

{

    

    struct Node* new_node

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

  

    struct Node* final = *head_ref;

  

    

    new_node->information = new_data;

  

    

       

    new_node->subsequent = NULL;

  

    

     

    if (*head_ref == NULL) {

        *head_ref = new_node;

        return;

    }

  

    

    whereas (last->subsequent != NULL)

        final = last->subsequent;

  

    

    last->subsequent = new_node;

    return;

}

Java

   

public void append(int new_data)

{

    

       

       

    Node new_node = new Node(new_data);

  

    

           

    if (head == null) {

        head = new Node(new_data);

        return;

    }

  

    

         

    new_node.subsequent = null;

  

    

    Node final = head;

    whereas (final.subsequent != null)

        final = final.subsequent;

  

    

    final.subsequent = new_node;

    return;

}

Python3

def append(self, new_data):

  

        

        

        

        new_node = Node(new_data)

  

        

        

        if self.head is None:

            self.head = new_node

            return

  

        

        final = self.head

        whereas (final.subsequent):

            final = final.subsequent

  

        

        final.subsequent = new_node

C#

public void append(int new_data)

{

    

    

    

    Node new_node = new Node(new_data);

  

    

       

    if (head == null) {

        head = new Node(new_data);

        return;

    }

  

    

    

    new_node.subsequent = null;

  

    

    Node final = head;

    whereas (final.subsequent != null)

        final = final.subsequent;

  

    

    final.subsequent = new_node;

    return;

}

Javascript

<script>

  

   

 perform append(new_data)

{

    

       

       

    var new_node = new Node(new_data);

  

    

           

    if (head == null)

    {

        head = new Node(new_data);

        return;

    }

  

    

         

    new_node.subsequent = null;

  

    

    var final = head; 

    whereas (final.subsequent != null)

        final = final.subsequent;

  

    

    final.subsequent = new_node;

    return;

}

  

</script>

Time Complexity: O(N) the place N is the size of the linked record
Auxiliary Area: O(1)

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *