# Removing Duplicates from Sorted Linked List

## Problem Statement

You are given a singly sorted linked list, which has repeated elements. The task at hand is to remove the duplicates from the linked list. Here is a diagram to explain the problem statement

## Solution for Removing Duplicates from Sorted Linked List

As the linked list is sorted, if two nodes contain duplicates, they will always be adjacent to each other. Hence, the trick is to
• Maintain two pointers (Current_Node_Ptr and Next_Node_Ptr).
• If the Next_Node_Ptr points to a duplicate node, delete the node and advance the Next_Node_Ptr
• Make the next of Current_Node_Ptr point to the Next_Node_Ptr after each deletion.
As per the above algorithm, we have to use two loops, the outer loop advances the Current_Node_Ptr and the inner loop deletes duplicate node and advances the two pointers.

## Source Code

```public static void removeDuplicates(Node L){

if(L == null)
return;
// L is the Current_Node_Ptr
// T is the Next_Node_Ptr
Node T = null;
while(L != null && L.next != null){
T = L.next;
while(T != null && (T.data == L.data)){
L.next = T.next;
T = T.next;
}
L = L.next;
}
}```