Saturday, September 21, 2024
Google search engine
HomeData Modelling & AIDelete multiple occurrences of key in Linked list using double pointer

Delete multiple occurrences of key in Linked list using double pointer

Given a singly linked list, delete all occurrences of a given key in it. For example, consider the following list. 

Input: 2 -> 2 -> 4 -> 3 -> 2
       Key to delete = 2
Output:  4 -> 3 

This is mainly an alternative of this post which deletes multiple occurrences of a given key using separate condition loops for head and remaining nodes. Here we use a double pointer approach to use a single loop irrespective of the position of the element (head, tail or between). The original method to delete a node from a linked list without an extra check for the head was explained by Linus Torvalds in his “25th Anniversary of Linux” TED talk. 

This article uses that logic to delete multiple recurrences of the key without an extra check for the head. Explanation: 1. Store address of head in a double pointer till we find a non “key” node. This takes care of the 1st while loop to handle the special case of the head. 2. If a node is not “key” node then store the address of node->next in pp. 3. if we find a “key” node later on then change pp (ultimately node->next) to point to current node->next. Following is C++ implementation for the same. 

Implementation:

C++




// CPP program to delete multiple
// occurrences of a key using single
// loop.
#include <iostream>
using namespace std;
 
// A linked list node
struct Node {
 int data;
 struct Node* next;
};
 
struct Node* head = NULL;
 
void printList(struct Node* node)
{
 while (node != NULL) {
  printf(" %d ", node->data);
  node = node->next;
 }
}
 
void push(int new_data)
{
 struct Node* new_node = new Node;
 new_node->data = new_data;
 new_node->next = (head);
 (head) = new_node;
}
 
void deleteEntry(int key)
{
 // Start from head
 struct Node** pp = &head;
 while (*pp) {
 
  struct Node* entry = *pp;
 
  // If key found, then put
  // next at the address of pp
  // delete entry.
  if (entry->data == key) {
   *pp = entry->next;
   delete (entry);
  }
 
  // Else move to next
  else
   pp = &(entry->next);
 }
}
 
int main()
{
 push(2);
 push(2);
 push(4);
 push(3);
 push(2);
 
 int key = 2; // key to delete
 
 puts("Created Linked List: ");
 printList(head);
 printf("\n");
 deleteEntry(key);
 printf("\nLinked List after Deletion of 2: \n");
 printList(head);
 return 0;
}


Output

Created Linked List: 
 2  3  4  2  2 

Linked List after Deletion of 2: 
 3  4 

Complexity Analysis:

  • Time Complexity: O(n)
  • Auxiliary Space: O(1), as no extra space is required
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments