VOOZH about

URL: https://www.geeksforgeeks.org/dsa/insert-node-at-the-end-of-a-linked-list/

⇱ Insert Node at the End of a Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insert Node at the End of a Linked List

Last Updated : 21 Apr, 2026

Given a head of the linked list, we need to insert a new node at the end of the linked list.

Examples:

Input: x = 6

👁 blobid2_1755948542

Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Explanation: We can see that 6 is inserted at the end of the linkedlist.

👁 blobid3_1755948555


Input: x = 1

👁 blobid0_1755948331

Output: 4 -> 5 -> 1
Explanation: We can see that 1 is inserted at the end of the linked list.

👁 blobid1_1755948342

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:


Output
1 -> 2 -> 3 -> 4 -> 5 -> 6

Time Complexity: O(n) where n is the length of the linked list
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: