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>usingnamespacestd;// Node class for the linked listclassNode{public:intdata;Node*next;Node(intx){this->data=x;this->next=nullptr;}};// Function to remove the last node// of the linked listNode*removeLastNode(Node*head){// If the list is empty, return nullptrif(head==nullptr){returnnullptr;}// If the list has only one node, delete it and return nullptrif(head->next==nullptr){deletehead;returnnullptr;}// Find the second last nodeNode*secondLast=head;while(secondLast->next->next!=nullptr){secondLast=secondLast->next;}// Delete the last nodedeletesecondLast->next;// Change next of second lastsecondLast->next=nullptr;returnhead;}voidprintList(Node*head){while(head!=nullptr){cout<<head->data<<" -> ";head=head->next;}cout<<"nullptr"<<endl;}intmain(){// Creating a static linked list// 1 -> 2 -> 3 -> 4 -> 5 -> nullptrNode*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);// Removing the last nodehead=removeLastNode(head);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>// Node structure for the linked liststructNode{intdata;structNode*next;};// Function to create a new nodestructNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}// Function to remove the last node of the linked liststructNode*removeLastNode(structNode*head){// If the list is emptyif(head==NULL)returnNULL;// If the list has only one nodeif(head->next==NULL){free(head);returnNULL;}// Find the second last nodestructNode*secondLast=head;while(secondLast->next->next!=NULL){secondLast=secondLast->next;}// Delete the last nodefree(secondLast->next);// Update next of second last nodesecondLast->next=NULL;returnhead;}// Function to print the linked listvoidprintList(structNode*head){while(head!=NULL){printf("%d",head->data);if(head->next!=NULL)printf(" -> ");head=head->next;}printf(" -> NULL\n");}intmain(){// Creating the linked list: 1 -> 2 -> 3 -> 4 -> 5structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(3);head->next->next->next=createNode(4);head->next->next->next->next=createNode(5);// Remove the last nodehead=removeLastNode(head);// Print the updated listprintList(head);return0;}
Java
classGfG{// Node class for the linked liststaticclassNode{intdata;Nodenext;Node(intx){this.data=x;this.next=null;}}// Function to remove the last // node of the linked liststaticNoderemoveLastNode(Nodehead){// If the list is empty, return nullif(head==null){returnnull;}// If the list has only one node, // delete it and return nullif(head.next==null){returnnull;}// Find the second last nodeNodesecondLast=head;while(secondLast.next.next!=null){secondLast=secondLast.next;}// Delete the last node by making // secondLast point to nullsecondLast.next=null;returnhead;}staticvoidprintList(Nodehead){while(head!=null){System.out.print(head.data+" -> ");head=head.next;}System.out.println("null");}publicstaticvoidmain(String[]args){// Creating a static linked list// 1 -> 2 -> 3 -> 4 -> 5 -> nullNodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Removing the last nodehead=removeLastNode(head);printList(head);}}
Python
# Node class for the linked listclassNode:def__init__(self,x):self.data=xself.next=None# Function to remove the last # node of the linked listdefremoveLastNode(head):# If the list is empty, return NoneifheadisNone:returnNone# If the list has only one node, # delete it and return Noneifhead.nextisNone:returnNone# Find the second last nodesecondLast=headwhilesecondLast.next.nextisnotNone:secondLast=secondLast.next# Delete the last node by making # second_last point to NonesecondLast.next=NonereturnheaddefprintList(head):whileheadisnotNone:print(f"{head.data} -> ",end="")head=head.nextprint("None")if__name__=="__main__":# Creating a static linked list# 1 -> 2 -> 3 -> 4 -> 5 -> Nonehead=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 nodehead=removeLastNode(head)printList(head)
C#
usingSystem;// Node class for the linked listclassNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Function to remove the last// node of the linked listpublicstaticNoderemoveLastNode(Nodehead){// If the list is empty, return nullif(head==null){returnnull;}// If the list has only one node, return nullif(head.next==null){returnnull;}// Find the second last nodeNodesecondLast=head;while(secondLast.next.next!=null){secondLast=secondLast.next;}// Delete the last node by making // secondLast point to nullsecondLast.next=null;returnhead;}publicstaticvoidprintList(Nodehead){while(head!=null){Console.Write(head.data+" -> ");head=head.next;}Console.WriteLine("null");}staticvoidMain(){// Creating a static linked list// 1 -> 2 -> 3 -> 4 -> 5 -> nullNodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Removing the last nodehead=removeLastNode(head);printList(head);}}
JavaScript
// Node class for the linked listclassNode{constructor(data){this.data=data;this.next=null;}}// Function to remove the last// node of the linked listfunctionremoveLastNode(head){// If the list is empty, return nullif(head===null){returnnull;}// If the list has only one // node, return nullif(head.next===null){returnnull;}// Find the second last nodeletsecondLast=head;while(secondLast.next.next!==null){secondLast=secondLast.next;}// Delete the last node by disconnecting itsecondLast.next=null;returnhead;}functionprintList(head){letcurrent=head;while(current!==null){process.stdout.write(current.data+" -> ");current=current.next;}console.log("null");}// Driver codefunctionmain(){// Creating a static linked list// 1 -> 2 -> 3 -> 4 -> 5 -> nulllethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);// Removing the last nodehead=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)