Insert Node at the End of a Linked List
Last Updated : 29 Jul, 2024
Given a linked list, the task is to insert a new node at the end of the linked list.
Examples:
Input: LinkedList = 2 -> 3 -> 4 -> 5, NewNode = 1
Output: LinkedList = 2 -> 3 -> 4 -> 5 -> 1
Input: LinkedList = NULL, NewNode = 1
Output: LinkedList = 1
Approach:
Inserting at the end involves traversing the entire list until we reach the last node. We then set the last node's next reference to point to the new node, making the new node the last element in the list.
Following is the approach to add a new node at the end of the linked list:
- Create a new node and set its next pointer as NULL since it will be the last node.
- Store the head reference in a temporary variable
- 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++ #include <iostream> using namespace std; // A linked list node class Node { public: int data; Node* next; // Constructor to initialize a new node with data Node(int new_data) { data = new_data; next = nullptr; } }; // Given the head of a list and an int, appends // a new node at the end and returns the head. Node* append(Node* head, int new_data) { // Create a new node Node* new_node = new Node(new_data); // If the Linked List is empty, make // the new node as the head and return if (head == nullptr) { return new_node; } // Store the head reference in a temporary variable Node* last = head; // Traverse till the last node while (last->next != nullptr) { last = last->next; } // Change the next pointer of the last node // to point to the new node last->next = new_node; // Return the head of the list return head; } // This function prints the contents // of the linked list starting from the head void printList(Node* node) { while (node != nullptr) { cout << " " << node->data; node = node->next; } } // Driver code int main() { // Create a hard-coded linked list: // 2 -> 3 -> 4 -> 5 -> 6 Node* head = new Node(2); head->next = new Node(3); head->next->next = new Node(4); head->next->next->next = new Node(5); head->next->next->next->next = new Node(6); cout << "Created Linked list is:"; printList(head); // Example of appending a node at the end head = 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; }; // Function to create a new node struct Node* createNode(int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; return new_node; } // Given the head of a list and an int, appends // a new node at the end and returns the head. struct Node* append(struct Node* head, int new_data) { // Create a new node struct Node* new_node = createNode(new_data); // If the Linked List is empty, make // the new node as the head and return if (head == NULL) { return new_node; } // Store the head reference in a temporary variable struct Node* last = head; // 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; // Return the head of the list return head; } // 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() { // Create a hard-coded linked list: // 2 -> 3 -> 4 -> 5 -> 6 struct Node* head = createNode(2); head->next = createNode(3); head->next->next = createNode(4); head->next->next->next = createNode(5); head->next->next->next->next = createNode(6); printf("Created Linked list is:"); printList(head); // Example of appending a node at the end head = append(head, 1); printf("\nAfter inserting 1 at the end:"); printList(head); return 0; }
Java class Node { int data; Node next; // Constructor to initialize a new node with data Node(int newData) { data = newData; next = null; } } public class GfG { // Given the head of a list and an int, appends // a new node at the end and returns the head. static Node append(Node head, int newData) { // Create a new node Node newNode = new Node(newData); // If the Linked List is empty, make the new // node as the head and return if (head == null) { return newNode; } // Store the head reference in a temporary variable Node last = head; // 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 = newNode; // Return the head of the list return head; } // This function prints the contents of // the linked list starting from the head public static void printList(Node node) { while (node != null) { System.out.print(" " + node.data); node = node.next; } } // Driver code public static void main(String[] args) { // Create a hard-coded linked list: // 2 -> 3 -> 4 -> 5 -> 6 Node head = new Node(2); head.next = new Node(3); head.next.next = new Node(4); head.next.next.next = new Node(5); head.next.next.next.next = new Node(6); System.out.print("Created Linked list is:"); printList(head); // Example of appending a node at the end head = append(head, 1); System.out.print("\nAfter inserting 1 at the end:"); printList(head); } }
Python class Node: def __init__(self, data): self.data = data self.next = None # Given the head of a list and an int, appends # a new node at the end and returns the head. def append(head, new_data): # Create a new node new_node = Node(new_data) # If the Linked List is empty, make the # new node as the head and return if head is None: return new_node # Store the head reference in a temporary variable last = head # Traverse till the last node while last.next: last = last.next # Change the next pointer of the last # node to point to the new node last.next = new_node # Return the head of the list return head # This function prints the contents of the # linked list starting from the head def print_list(node): while node: print(node.data, end=" ") node = node.next # Driver code if __name__ == "__main__": # Create a hard-coded linked list: # 2 -> 3 -> 4 -> 5 -> 6 head = Node(2) head.next = Node(3) head.next.next = Node(4) head.next.next.next = Node(5) head.next.next.next.next = Node(6) print("Created Linked list is: ", end="") print_list(head) # Example of appending a node at the end head = append(head, 1) print("\nAfter inserting 1 at the end: ", end="") print_list(head)
C# using System; public class Node { public int data; public Node next; // Constructor to initialize a new node with data public Node(int newData) { data = newData; next = null; } } // Appends a new node at the end // and returns the head. public class GfG { public static Node Append(Node head, int newData) { // Create a new node Node newNode = new Node(newData); // If the Linked List is empty, // make the new node as the head if (head == null) { return newNode; } // Store the head reference in a // temporary variable Node last = head; // 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 = newNode; // Return the head of the list return head; } // 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) { // Create a hard-coded linked list: // 2 -> 3 -> 4 -> 5 -> 6 Node head = new Node(2); head.next = new Node(3); head.next.next = new Node(4); head.next.next.next = new Node(5); head.next.next.next.next = new Node(6); Console.Write("Created Linked list is: "); PrintList(head); // Example of appending a node // at the end head = Append(head, 1); Console.Write("\nAfter inserting 1 at the end: "); PrintList(head); } }
JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } // Appends a new node at the end // and returns the head. function append(head, newData) { // Create a new node const newNode = new Node(newData); // If the Linked List is empty, // make the new node as the head if (head === null) { return newNode; } // Store the head reference in a // temporary variable let last = head; // 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 = newNode; // Return the head of the list return head; } // Prints the contents of the linked // list starting from the head function printList(node) { while (node !== null) { console.log(node.data + " "); node = node.next; } } // Create a hard-coded linked list: // 2 -> 3 -> 4 -> 5 -> 6 let head = new Node(2); head.next = new Node(3); head.next.next = new Node(4); head.next.next.next = new Node(5); head.next.next.next.next = new Node(6); console.log("Created Linked list is:"); printList(head); // Example of appending a node // at the end head = append(head, 1); console.log("\nAfter inserting 1 at the end:"); printList(head);
OutputCreated 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)
Similar Reads
Insert a Node at the end of Doubly Linked List Given a Doubly Linked List, the task is to insert a new node at the end of the linked list.Examples: Input: Linked List = 1 <-> 2 <-> 3, NewNode = 4Output: Linked List = 1 <-> 2 <-> 3 <-> 4Input: Linked List = NULL, NewNode = 1Output: Linked List = 1Approach: Inserting
9 min read
Insert a Node after a given Node in Linked List Given a linked list, the task is to insert a new node after a given node of the linked list. If the given node is not present in the linked list, print "Node not found".Examples:Input: LinkedList = 2 -> 3 -> 4 -> 5, newData = 1, key = 2Output: LinkedList = 2 -> 1 -> 3 -> 4 -> 5I
11 min read
Insert a Node at Front/Beginning of a Linked List Given a linked list, the task is to insert a new node at the beginning/start/front of the linked list.Example:Input: LinkedList = 2->3->4->5, NewNode = 1Output: 1 2 3 4 5Input: LinkedList = 2->10, NewNode = 1Output: 1 2 10Approach:To insert a new node at the front, we create a new node a
8 min read
Insert a node in Linked List before a given node Given a linked list, the task is to insert a new node with a specified value into a linked list before a node with a given key.ExamplesInput: head: 1 -> 2 -> 3 -> 4 -> 5 , newData = 6, key = 2Output: 1 -> 6 -> 2 -> 3 -> 4 -> 5 Explanation: After inserting node with value 6
15+ min read
Insert a node at a specific position in a linked list Given a singly linked list, a position pos, and data, the task is to insert that data into a linked list at the given position. Examples:Input: 3->5->8->10, data = 2, pos = 2Output: 3->2->5->8->10Input: 3->5->8->10, data = 11, pos = 5Output: 3->5->8->10->11
8 min read
Insert a Node after a given node in Doubly Linked List Given a Doubly Linked List, the task is to insert a new node after a given node in the linked list.Examples: Input: Linked List = 1 <-> 2 <-> 4, newData = 3, key = 2Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node 3 is inserted after key, that is node 2.In
11 min read