Open In App

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

Last Updated : 29 Aug, 2025
Suggest changes
Share
30 Likes
Like
Report

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:  

C++
#include <iostream> using namespace std; // Node class for the linked list class Node { public:  int data;  Node* next;  Node(int x) {  this->data = x;  this->next = nullptr;  } }; // Function to remove the last node // of the linked list Node* removeLastNode(Node* head) {  // If the list is empty, return nullptr  if (head == nullptr) {  return nullptr;  }  // If the list has only one node, delete it and return nullptr  if (head->next == nullptr) {  delete head;  return nullptr;  }  // Find the second last node  Node* secondLast = head;  while (secondLast->next->next != nullptr) {  secondLast = secondLast->next;  }  // Delete the last node  delete secondLast->next;  // Change next of second last  secondLast->next = nullptr;  return head; } void printList(Node* head) {  while (head != nullptr) {  cout << head->data << " -> ";  head = head->next;  }  cout << "nullptr" << endl; } int main() {  // Creating a static linked list  // 1 -> 2 -> 3 -> 4 -> 5 -> nullptr  Node* head = new Node(1);  head->next = new Node(2);  head->next->next = new Node(3);  head->next->next->next = new Node(4);  head->next->next->next->next = new Node(5);  // Removing the last node  head = removeLastNode(head);  printList(head);  return 0; } 
Java
class GfG {  // Node class for the linked list  static class Node {  int data;  Node next;  Node(int x) {  this.data = x;  this.next = null;  }  }  // Function to remove the last   // node of the linked list  static Node removeLastNode(Node head) {  // If the list is empty, return null  if (head == null) {  return null;  }  // If the list has only one node,   // delete it and return null  if (head.next == null) {  return null;  }  // Find the second last node  Node secondLast = head;  while (secondLast.next.next != null) {  secondLast = secondLast.next;  }  // Delete the last node by making   // secondLast point to null  secondLast.next = null;  return head;  }  static void printList(Node head) {  while (head != null) {  System.out.print(head.data + " -> ");  head = head.next;  }  System.out.println("null");  }  public static void main(String[] args) {  // Creating a static linked list  // 1 -> 2 -> 3 -> 4 -> 5 -> null  Node head = new Node(1);  head.next = new Node(2);  head.next.next = new Node(3);  head.next.next.next = new Node(4);  head.next.next.next.next = new Node(5);  // Removing the last node  head = removeLastNode(head);  printList(head);  } } 
Python
# Node class for the linked list class Node: def __init__(self, x): self.data = x self.next = None # Function to remove the last  # node of the linked list def removeLastNode(head): # If the list is empty, return None if head is None: return None # If the list has only one node,  # delete it and return None if head.next is None: return None # Find the second last node secondLast = head while secondLast.next.next is not None: secondLast = secondLast.next # Delete the last node by making  # second_last point to None secondLast.next = None return head def printList(head): while head is not None: print(f"{head.data} -> ", end="") head = head.next print("None") if __name__ == "__main__": # Creating a static linked list # 1 -> 2 -> 3 -> 4 -> 5 -> None head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5) # Removing the last node head = removeLastNode(head) printList(head) 
C#
using System; // Node class for the linked list class Node {  public int data;  public Node next;  public Node(int data) {  this.data = data;  this.next = null;  } } class GfG {  // Function to remove the last  // node of the linked list  public static Node removeLastNode(Node head) {  // If the list is empty, return null  if (head == null) {  return null;  }  // If the list has only one node, return null  if (head.next == null) {  return null;  }  // Find the second last node  Node secondLast = head;  while (secondLast.next.next != null) {  secondLast = secondLast.next;  }  // Delete the last node by making   // secondLast point to null  secondLast.next = null;  return head;  }  public static void printList(Node head) {  while (head != null) {  Console.Write(head.data + " -> ");  head = head.next;  }  Console.WriteLine("null");  }  static void Main() {  // Creating a static linked list  // 1 -> 2 -> 3 -> 4 -> 5 -> null  Node head = new Node(1);  head.next = new Node(2);  head.next.next = new Node(3);  head.next.next.next = new Node(4);  head.next.next.next.next = new Node(5);  // Removing the last node  head = removeLastNode(head);  printList(head);  } } 
JavaScript
// Node class for the linked list class Node {  constructor(data) {  this.data = data;  this.next = null;  } } // Function to remove the last // node of the linked list function removeLastNode(head) {  // If the list is empty, return null  if (head === null) {  return null;  }  // If the list has only one   // node, return null  if (head.next === null) {  return null;  }  // Find the second last node  let secondLast = head;  while (secondLast.next.next !== null) {  secondLast = secondLast.next;  }  // Delete the last node by disconnecting it  secondLast.next = null;  return head; } function printList(head) {  let current = head;  while (current !== null) {  process.stdout.write(current.data + " -> ");  current = current.next;  }  console.log("null"); } // Driver code function main() {  // Creating a static linked list  // 1 -> 2 -> 3 -> 4 -> 5 -> null  let head = new Node(1);  head.next = new Node(2);  head.next.next = new Node(3);  head.next.next.next = new Node(4);  head.next.next.next.next = new Node(5);  // Removing the last node  head = removeLastNode(head);  printList(head); } main(); 

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)


Article Tags :

Explore