Remove duplicates from a sorted linked list using recursion

Write a removeDuplicates() function which takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.

For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60.

Algorithm:
Traverse the list recursively from the head (or start) to end and after completion of recursion calls, compare the next node(returned node) and current node(head). If data of both nodes are equal then return the next (head-> next) node else return the current node(head).

Implementation:
Functions other than removeDuplicates() are just to create a linked linked list and test removeDuplicates().

/* C Program to remove duplicates
 from a sorted linked list */
#include <bits/stdc++.h>
#include <stdlib.h>

/* Link list node */
struct Node {
    int data;
    struct Node* next;
};

/* The function removes duplicates from a sorted list */
struct Node* removeDuplicates(struct Node* head)
{
    /* if head is null then return*/
    if (head == NULL)
        return NULL;

    /* Remove duplicates from list after head */
    head->next = removeDuplicates(head->next);

    // Check if head itself is duplicate
    if (head->next != NULL && 
        head->next->data == head->data) {

        Node* res = head->next;
        delete head;
        return res;
    }

    return head;
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at 
   the beginning of the linked list */
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node = new Node;
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}

/* Drier program to test above functions*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;

    /* Let us create a sorted linked list to test the functions
    Created linked list will be 11->11->11->13->13->20 */
    push(&head, 20);
    push(&head, 13);
    push(&head, 13);
    push(&head, 11);
    push(&head, 11);
    push(&head, 11);

    printf("\n Linked list before duplicate removal ");
    printList(head);

    /* Remove duplicates from linked list */
    struct Node* h = removeDuplicates(head);

    printf("\n Linked list after duplicate removal ");
    printList(h);

    return 0;
}
Output:

Linked list before duplicate removal 11 11 11 13 13 20 
 Linked list after duplicate removal 11 13 20


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




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

Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 3 vote(s)






User Actions