Insert Node on the Finish of a Linked Checklist

[ad_1]

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. Not like arrays, linked listing parts should 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 tackle of the subsequent node.

To study extra about linked listing seek advice from the article “Introduction to Linked LIst“.

Given a linked listing, the duty is to insert a brand new node on the finish of the linked listing.

Insert a node at the end of a linked list

Insert a node on the finish of a linked listing

Instance:

Checklist = 1->2->3->4, Insert a node with worth 5 on the finish.
Output listing can be: 1->2->3->4->5

Think about the next representations of the linked listing. 

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: Following is the strategy so as to add a brand new node on the finish of the linked listing:

  • Allocate reminiscence for a brand new node (say temp) and create a pointer (say final) which factors the top of the linked listing.
  • 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 listing is empty make temp because the head.
  • Traverse utilizing the final pointer until we attain the tip node of the linked listing.
  • Now, level the subsequent node of final to temp.

Comply with 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 listing

Under 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->knowledge = 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->knowledge = 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 listing
Auxiliary Area: O(1)

[ad_2]

Leave a Reply