The idea is to traverse the linked list from beginning to end. While traversing, keep track of last occurrence key node and previous node of that key. After traversing the complete list, delete the last occurrence of that key.
Follow the steps below to solve the problem:
Initialize Pointer curr points to head, last, lastPrev and prev to NULL.
Traverse the List until curr is not NULL:
If curr->data == key, update lastPrev to the prev and last to curr.
Move prev pointer to curr and curr to the next node.
Delete Last Occurrence (if key was found then last is not null):
If lastPrev is not null, adjust lastPrev->next = last->next to skip last.
If last is the head, update the head to last->next.
C++
// C++ program to delete last occurrence // of key in singly linked list#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function to delete the last occurrence // of a key in the linked listNode*deleteLastOccurrence(Node*head,intkey){Node*last=nullptr,*lastPrev=nullptr;Node*curr=head,*prev=nullptr;// Traverse the list to find the last // occurrence of the keywhile(curr!=nullptr){if(curr->data==key){lastPrev=prev;last=curr;}prev=curr;curr=curr->next;}// If the key was foundif(last!=nullptr){// If last occurrence is not the headif(lastPrev!=nullptr){lastPrev->next=last->next;}else{// If last occurrence is the head, // move head to next nodehead=head->next;}deletelast;}returnhead;}voidprint(Node*curr){while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;}intmain(){// Create a hard-coded linked list:// 1 -> 2 -> 2 -> 4 -> 2Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(2);head->next->next->next=newNode(4);head->next->next->next->next=newNode(2);intkey=2;head=deleteLastOccurrence(head,key);print(head);return0;}
C
// C program to delete last occurrence // of key in singly linked list#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Function to delete the last occurrence// of a key in the linked liststructNode*deleteLastOccurrence(structNode*head,intkey){structNode*last=NULL,*lastPrev=NULL;structNode*curr=head,*prev=NULL;// Traverse the list to find the last// occurrence of the keywhile(curr!=NULL){if(curr->data==key){lastPrev=prev;last=curr;}prev=curr;curr=curr->next;}// If the key was foundif(last!=NULL){// If last occurrence is not the headif(lastPrev!=NULL){lastPrev->next=last->next;}else{// If last occurrence is the head,// move head to next nodehead=head->next;}free(last);}returnhead;}voidprint(structNode*curr){while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}printf("\n");}structNode*createNode(intnew_data){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=new_data;newNode->next=NULL;returnnewNode;}intmain(){// Create a hard-coded linked list:// 1 -> 2 -> 2 -> 4 -> 2structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(2);head->next->next->next=createNode(4);head->next->next->next->next=createNode(2);intkey=2;head=deleteLastOccurrence(head,key);print(head);return0;}
Java
// Java program to delete last occurrence // of key in singly linked listclassNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGfG{// Function to delete the last occurrence // of a key in the linked liststaticNodedeleteLastOccurrence(Nodehead,intkey){Nodelast=null,lastPrev=null;Nodecurr=head,prev=null;// Traverse the list to find the last // occurrence of the keywhile(curr!=null){if(curr.data==key){lastPrev=prev;last=curr;}prev=curr;curr=curr.next;}// If the key was foundif(last!=null){// If last occurrence is not the headif(lastPrev!=null){lastPrev.next=last.next;}else{// If last occurrence is the head, // move head to next nodehead=head.next;}}returnhead;}staticvoidprint(Nodecurr){while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){// Create a hard-coded linked list:// 1 -> 2 -> 2 -> 4 -> 2Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(2);head.next.next.next=newNode(4);head.next.next.next.next=newNode(2);intkey=2;head=deleteLastOccurrence(head,key);print(head);}}
Python
# Python program to delete last occurrence # of key in singly linked listclassNode:def__init__(self,x):self.data=xself.next=None# Function to delete the last occurrence# of a key in the linked listdefdeleteLastOccurrence(head,key):last=NonelastPrev=Nonecurr=headprev=None# Traverse the list to find the last# occurrence of the keywhilecurrisnotNone:ifcurr.data==key:lastPrev=prevlast=currprev=currcurr=curr.next# If the key was foundiflastisnotNone:# If last occurrence is not the headiflastPrevisnotNone:lastPrev.next=last.nextelse:# If last occurrence is the head,# move head to next nodehead=head.nextreturnheaddefprintList(curr):whilecurrisnotNone:print(curr.data,end=" ")curr=curr.nextprint()if__name__=="__main__":# Create a hard-coded linked list:# 1 -> 2 -> 2 -> 4 -> 2head=Node(1)head.next=Node(2)head.next.next=Node(2)head.next.next.next=Node(4)head.next.next.next.next=Node(2)key=2head=deleteLastOccurrence(head,key)printList(head)
C#
// C# program to delete last occurrence // of key in singly linked listclassNode{publicintdata;publicNodenext;publicNode(intnew_data){data=new_data;next=null;}}classGfG{// Function to delete the last occurrence // of a key in the linked liststaticNodedeleteLastOccurrence(Nodehead,intkey){Nodelast=null,lastPrev=null;Nodecurr=head,prev=null;// Traverse the list to find the last // occurrence of the keywhile(curr!=null){if(curr.data==key){lastPrev=prev;last=curr;}prev=curr;curr=curr.next;}// If the key was foundif(last!=null){// If last occurrence is not the headif(lastPrev!=null){lastPrev.next=last.next;}else{// If last occurrence is the head, // move head to next nodehead=head.next;}}returnhead;}staticvoidprint(Nodecurr){while(curr!=null){System.Console.Write(curr.data+" ");curr=curr.next;}System.Console.WriteLine();}staticvoidMain(string[]args){// Create a hard-coded linked list:// 1 -> 2 -> 2 -> 4 -> 2Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(2);head.next.next.next=newNode(4);head.next.next.next.next=newNode(2);intkey=2;head=deleteLastOccurrence(head,key);print(head);}}
JavaScript
// Javascript program to delete last occurrence // of key in singly linked listclassNode{constructor(new_data){this.data=new_data;this.next=null;}}// Function to delete the last occurrence// of a key in the linked listfunctiondeleteLastOccurrence(head,key){letlast=null,lastPrev=null;letcurr=head,prev=null;// Traverse the list to find the last// occurrence of the keywhile(curr!==null){if(curr.data===key){lastPrev=prev;last=curr;}prev=curr;curr=curr.next;}// If the key was foundif(last!==null){// If last occurrence is not the headif(lastPrev!==null){lastPrev.next=last.next;}else{// If last occurrence is the head,// move head to next nodehead=head.next;}}returnhead;}functionprintList(curr){while(curr!==null){console.log(curr.data+" ");curr=curr.next;}}// Create a hard-coded linked list:// 1 -> 2 -> 2 -> 4 -> 2lethead=newNode(1);head.next=newNode(2);head.next.next=newNode(2);head.next.next.next=newNode(4);head.next.next.next.next=newNode(2);letkey=2;head=deleteLastOccurrence(head,key);printList(head);
Output
1 2 2 4
Time Complexity: O(n), Traversing over the linked list of size n. Auxiliary Space: O(1)