VOOZH about

URL: https://www.geeksforgeeks.org/dsa/remove-last-node-of-the-linked-list/

⇱ Deletion at end (Removal of last node) in a Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deletion at end (Removal of last node) in a Linked List

Last Updated : 21 Oct, 2025

Given the head of a linked list, delete the last node of the given linked list.

Examples:

Input:

👁 blobid2_1755947119

Output:

👁 blobid3_1755947241

Explanation: The last node of the linked list is 5, so 5 is deleted.

Input:

👁 blobid0_1755945539

Output: 3 -> 12 -> NULL

👁 blobid1_1755945555

Explanation: The last node of the linked list is 15, so 15 is deleted.

Approach:

To perform the deletion operation at the end of linked list, we need to traverse the list to find the second last node, then set its next pointer to null. If the list is empty then there is no node to delete or has only one node then point head to null.


Step-by-step approach:

  • Check if list is empty then return NULL.
  • If the list has only one node then delete it and return NULL.
  • Traverse the list to find the second last node.
    => Set the next pointer of second last node to NULL.

Implementation:


Output
1 -> 2 -> 3 -> 4 -> nullptr

Time Complexity: O(n), traversal of the linked list till its end, so the time complexity required is O(n).
Auxiliary Space: O(1)

Comment
Article Tags: