Open In App

Delete last occurrence of an item from linked list

Last Updated : 20 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a singly linked list and a key, the task is to delete the last occurrence of that key in the linked list.

Examples

Input: head: 1 -> 2 -> 3 ->1 -> 4 -> NULL, key = 1 
Output: 1 -> 2 -> 3 -> 4 -> NULL

Delete-last-occurrence-of-an-item-from-linked-list-1


Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULL , key = 3
Output: 1 -> 2 -> 4 -> 5 -> NULL

Delete-last-occurrence-of-an-item-from-linked-list-2

Approach:

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>
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

// Function to delete the last occurrence 
// of a key in the linked list
Node* deleteLastOccurrence(Node* head, int key) {
    Node *last = nullptr, *lastPrev = nullptr;
    Node *curr = head, *prev = nullptr;

    // Traverse the list to find the last 
  	// occurrence of the key
    while (curr != nullptr) {
        if (curr->data == key) {
            lastPrev = prev;
            last = curr;
        }
        prev = curr;
        curr = curr->next;
    }

    // If the key was found
    if (last != nullptr) {
        
      	// If last occurrence is not the head
        if (lastPrev != nullptr) {
            lastPrev->next = last->next;
        } else {
          
            // If last occurrence is the head, 
          	// move head to next node
            head = head->next;
        }
        delete last;
    }

    return head;
}

void print(Node* curr) {
    while (curr != nullptr) {
        cout << curr->data << " ";
        curr = curr->next;
    }
    cout << endl;
}

int main() {
  
    // Create a hard-coded linked list:
    // 1 -> 2 -> 2 -> 4 -> 2
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(2);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(2);

    int key = 2;
    head = deleteLastOccurrence(head, key);
    print(head);
    return 0;
}
C Java Python C# JavaScript

Output
1 2 2 4 

Time Complexity: O(n), Traversing over the linked list of size n. 
Auxiliary Space: O(1)



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg