# Remove Duplicates from a Sorted Linked List Using Recursion

A linked list is a sequence of elements that are connected together. Each list have a head and series of nodes and each node has data of the current node and link to next node. The basic operations of a linked list are insertion, deletion, search and delete.

## Removing duplicates from a sorted linked list

One way to remove nodes from is using recursion. The idea is to compare each node with its adjacent node and delete the duplicate one they are equal.

Our recursive call will return us to the next node. So for the next element, we will call our recursive function like current_node->next = our_function(node->next).

We trust our recursion that the current_node->next now contains the linked list, which doesn't have any duplicate elements.

In the main method we are biding the list from scratch as −

Node* head = new Node(5);
head->next->next->next->next->next = new Node(7); 

### Algorithm

The procedure followed by the method to remove duplicates from a sorted linked list using recursion is as follows.

• Step 1 − A linked list is created with all the values sorted sequentially

• Step 2 − If the linked list does not exist, the program is terminated.

• Step 3 − If the linked list does exist, the next value of the head node is compared with the value in the head node. If the two values are the same, the head is deleted.

• Step 4 − The Step 3 is performed recursively, considering each node as head until the list removes all the duplicate values from itself.

• Step 5 − The output obtained is a sorted linked list with distinct values

## Example

For example, we have a sorted linked list with the following values −

1->1->1->2->3->3->4

Let us look at a C++ program that will remove duplicates from the above sorted linked list using recursion −

#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
next = NULL;
}
};
return NULL;
return temp;
}
}
void printList(Node* node) {
while (node != NULL) {
cout << node->data << (node->next == NULL ? "" : "->");
node = node->next;
}
}
int main() {
cout << "Linked list before: ";
cout << "\nLinked list after: ";
return 0;
}


After this, we check whether or not to include the current node into the linked list. If the satisfied linked list we got from current node->next has the same value as this node, we don't include this; otherwise, we include.

Note − When the current node is NULL, we return our base condition of recursion.

## Output

Linked list before: 1->1->1->2->3->3->4