Insert Node at the End of a Linked List
Given a linked list, the task is to insert a new node at the end of the linked list.

Insert a node at the end of a linked list
Example:
Input: LinkedList = 2->3->4->5, NewNode = 1
Output: LinkedList = 2->3->4->5->1Input: LinkedList = , NewNode = 1
Output: LinkedList = 1
Approach:
To insert a node at the end of a Linked List, we need to:
- Go to the last node of the Linked List
- Change the next pointer of last node from NULL to the new node
- Make the next pointer of new node as NULL to show the end of Linked List
Following is the approach to add a new node at the end of the linked list:
- Create a new node
- Store the head reference in a temporary variable
- Set the next pointer of the new node as NULL since it will be the last node
- If the Linked List is empty, make the new node as the head and return
- Else traverse till the last node
- Change the next pointer of the last node to point to the new node
Below is the implementation of the approach:
C++
// C++ program to demonstrate inserting a node // at the end of a Linked List #include <bits/stdc++.h> using namespace std; // A linked list node class Node { public: int data; Node* next; }; // Given a reference (pointer to pointer) // to the head of a list and an int, inserts // a new node at the front of the list. void push(Node** head_ref, int new_data) { // Create a new node Node* new_node = new Node(); new_node->data = new_data; // Make the new node point to the current head new_node->next = (*head_ref); // Update the head to point to the new node (*head_ref) = new_node; } // Given a reference (pointer to pointer) // to the head of a list and an int, // appends a new node at the end void append(Node** head_ref, int new_data) { // Create a new node Node* new_node = new Node(); new_node->data = new_data; // Store the head reference in a temporary variable Node* last = *head_ref; // Set the next pointer of the new node as NULL since it // will be the last node new_node->next = NULL; // If the Linked List is empty, make the new node as the // head and return if (*head_ref == NULL) { *head_ref = new_node; return; } // Else traverse till the last node while (last->next != NULL) { last = last->next; } // Change the next pointer of the last node to point to // the new node last->next = new_node; } // This function prints the contents of // the linked list starting from the head void printList(Node* node) { while (node != NULL) { cout << " " << node->data; node = node->next; } } // Driver code int main() { // Start with an empty list Node* head = NULL; // Insert nodes at the beginning of the linked list push(&head, 6); push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); cout << "Created Linked list is: "; printList(head); // Insert 1 at the end append(&head, 1); cout << "\nAfter inserting 1 at the end: "; printList(head); return 0; }
C
#include <stdio.h> #include <stdlib.h> // A linked list node struct Node { int data; struct Node* next; }; // Given a reference (pointer to pointer) // to the head of a list and an int, inserts // a new node on the front of the list. void push(struct Node** head_ref, int new_data) { // Create a new node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; // Make the new node point to the current head new_node->next = (*head_ref); // Update the head to point to the new node (*head_ref) = new_node; } // Given a reference (pointer to pointer) // to the head of a list and an int, // appends a new node at the end void append(struct Node** head_ref, int new_data) { // Create a new node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; // Store the head reference in a temporary variable struct Node* last = *head_ref; // Set the next pointer of the new node as NULL since it // will be the last node new_node->next = NULL; // If the Linked List is empty, make the new node as the // head and return if (*head_ref == NULL) { *head_ref = new_node; return; } // Else traverse till the last node while (last->next != NULL) { last = last->next; } // Change the next pointer of the last node to point to // the new node last->next = new_node; } // This function prints the contents of // the linked list starting from the head void printList(struct Node* node) { while (node != NULL) { printf(" %d", node->data); node = node->next; } } // Driver code int main() { // Start with an empty list struct Node* head = NULL; // Insert nodes at the beginning of the linked list push(&head, 6); push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); printf("Created Linked list is: "); printList(head); // Insert 1 at the end append(&head, 1); printf("\nAfter inserting 1 at the end: "); printList(head); return 0; }
Java
class Node { int data; Node next; Node(int data) { this.data = data; next = null; } } class LinkedList { Node head; // Inserts a new node at the front of the list void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } // Appends a new node at the end of the list void append(int new_data) { Node new_node = new Node(new_data); if (head == null) { head = new_node; return; } Node last = head; while (last.next != null) { last = last.next; } last.next = new_node; } // Prints the contents of the linked list void printList() { Node node = head; while (node != null) { System.out.print(node.data + " "); node = node.next; } } } public class Main { public static void main(String[] args) { LinkedList linkedList = new LinkedList(); // Insert nodes at the beginning of the linked list linkedList.push(6); linkedList.push(5); linkedList.push(4); linkedList.push(3); linkedList.push(2); System.out.print("Created Linked list is: "); linkedList.printList(); // Insert 1 at the end linkedList.append(1); System.out.print( "\nAfter inserting 1 at the end: "); linkedList.printList(); } }
Python3
# A linked list node class Node: def __init__(self, data): self.data = data self.next = None # Given a reference (pointer to pointer) # to the head of a list and an int, inserts # a new node on the front of the list. def push(head_ref, new_data): # Create a new node new_node = Node(new_data) # Make the new node point to the current head new_node.next = head_ref # Update the head to point to the new node return new_node # Given a reference (pointer to pointer) # to the head of a list and an int, # appends a new node at the end def append(head_ref, new_data): # Create a new node new_node = Node(new_data) # Store the head reference in a temporary variable last = head_ref # Set the next pointer of the new node as None since it # will be the last node new_node.next = None # If the Linked List is empty, make the new node as the # head and return if head_ref is None: return new_node # Else traverse till the last node while last.next is not None: last = last.next # Change the next pointer of the last node to point to # the new node last.next = new_node return head_ref # This function prints the contents of # the linked list starting from the head def printList(node): while node is not None: print(node.data, end=" ") node = node.next # Driver code if __name__ == "__main__": # Start with an empty list head = None # Insert nodes at the beginning of the linked list head = push(head, 6) head = push(head, 5) head = push(head, 4) head = push(head, 3) head = push(head, 2) print("Created Linked list is:") printList(head) # Insert 1 at the end head = append(head, 1) print("\nAfter inserting 1 at the end:") printList(head)
C#
using System; public class Node { public int data; public Node next; } public class LinkedList { // Given a reference (pointer to pointer) // to the head of a list and an int, inserts // a new node on the front of the list. public static void Push(ref Node head_ref, int new_data) { // Create a new node Node new_node = new Node(); new_node.data = new_data; // Make the new node point to the current head new_node.next = head_ref; // Update the head to point to the new node head_ref = new_node; } // Given a reference (pointer to pointer) // to the head of a list and an int, // appends a new node at the end public static void Append(ref Node head_ref, int new_data) { // Create a new node Node new_node = new Node(); new_node.data = new_data; // If the Linked List is empty, make the new node as // the head and return if (head_ref == null) { head_ref = new_node; return; } // Else traverse till the last node Node last = head_ref; while (last.next != null) { last = last.next; } // Change the next pointer of the last node to point // to the new node last.next = new_node; } // This function prints the contents of // the linked list starting from the head public static void PrintList(Node node) { while (node != null) { Console.Write(" " + node.data); node = node.next; } } // Driver code public static void Main(string[] args) { // Start with an empty list Node head = null; // Insert nodes at the beginning of the linked list Push(ref head, 6); Push(ref head, 5); Push(ref head, 4); Push(ref head, 3); Push(ref head, 2); Console.Write("Created Linked list is:"); PrintList(head); // Insert 1 at the end Append(ref head, 1); Console.Write("\nAfter inserting 1 at the end:"); PrintList(head); } }
Output
Created Linked list is: 2 3 4 5 6 After inserting 1 at the end: 2 3 4 5 6 1
Time Complexity: O(N) where N is the length of the linked list
Auxiliary Space: O(1)
Please Login to comment...