Delete adjacent duplicate nodes from the Doubly Linked List

Given a doubly linked list. The problem is to remove all adjacent duplicate nodes from the list such that the final modified doubly linked list does not contain any adjacent duplicate nodes.

Examples:

Approach: The approach uses stack to keep track of the adjacent nodes at any point in the modified Doubly Linked List.

Algorithm:

delAdjacentDuplicates(head_ref)
     Create an empty stack st
     Declare current, next, top
     current = head_ref
     while current != NULL
         if isEmpty(st) or current->data != peek(st)->data
             push current on to the stack st
             current = current->next
         else
             next = current->next
             top = peek(st)
             pop element from st
             delete node 'current'
             delete node 'top'
             current = next

peek(st) operation returns the value at the top of the stack. The algorithm to delete a node n from the doubly linked list using pointer to the node n is diccussed in this post.

filter_none

edit
close

play_arrow

link
brightness_4
code

/* C++ implementation to delete adjacent duplicate nodes 
   from the Doubly Linked List */
#include <bits/stdc++.h>
  
using namespace std;
  
/* a node of the doubly linked list */
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};
  
/* Function to delete a node in a Doubly Linked List.
   head_ref --> pointer to head node pointer.
   del  -->  pointer to node to be deleted. */
void deleteNode(struct Node** head_ref, struct Node* del)
{
    /* base case */
    if (*head_ref == NULL || del == NULL)
        return;
  
    /* If node to be deleted is head node */
    if (*head_ref == del)
        *head_ref = del->next;
  
    /* Change next only if node to be deleted is NOT the last node */
    if (del->next != NULL)
        del->next->prev = del->prev;
  
    /* Change prev only if node to be deleted is NOT the first node */
    if (del->prev != NULL)
        del->prev->next = del->next;
  
    /* Finally, free the memory occupied by del*/
    free(del);
}
  
/* function to delete adjacent duplicate nodes from
   the Doubly Linked List */
void delAdjacentDupNodes(struct Node** head_ref)
{
    // an empty stack 'st'
    stack<Node*> st;
  
    struct Node* current = *head_ref;
  
    /* traverse the doubly linked list */
    while (current != NULL) {
        /* if stack 'st' is empty or if current->data != st.top()->data
           push 'current' on to the stack 'st' */
        if (st.empty() || current->data != st.top()->data) {
            st.push(current);
  
            /* move to the next node */
            current = current->next;
        }
  
        // else current->data == st.top()->data
        else {
            /* pointer to the node next to the 'current' node */
            struct Node* next = current->next;
  
            /* pointer to the node at the top of 'st' */
            struct Node* top = st.top();
  
            /* remove top element from 'st' */
            st.pop();
  
            /* delete 'current' node from the list */
            deleteNode(head_ref, current);
  
            /* delete 'top' node from the list */
            deleteNode(head_ref, top);
  
            /* update 'current' */
            current = next;
        }
    }
}
  
/* Function to insert a node at the beginning 
   of the Doubly Linked List */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  
    /* put in the data  */
    new_node->data = new_data;
  
    /* since we are adding at the begining,
    prev is always NULL */
    new_node->prev = NULL;
  
    /* link the old list off the new node */
    new_node->next = (*head_ref);
  
    /* change prev of head node to new node */
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
  
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
  
/* Function to print nodes in a given doubly linked list */
void printList(struct Node* head)
{
    if (head == NULL)
        cout << "Empty Doubly Linked List";
  
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
/* Driver program to test above functions*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
  
    /* Create the doubly linked list 10<->8<->4<->4<->8<->5 */
    push(&head, 5);
    push(&head, 8);
    push(&head, 4);
    push(&head, 4);
    push(&head, 8);
    push(&head, 10);
  
    cout << "Doubly linked list before deletion:n";
    printList(head);
  
    /* delete adjacent duplicate nodes */
    delAdjacentDupNodes(&head);
  
    cout << "nDoubly linked list after deletion:n";
    printList(head);
  
    return 0;
}

chevron_right


Output:

Doubly linked list before deletion:
10 8 4 4 8 5
Doubly linked list after deletion:
10 5

Time Complexity: O(n)
Auxiliary Space: O(n), in worst case when there are no adjacent duplicate nodes.

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.