DEV Community

VARUN
VARUN

Posted on

CA 23 - Remove Duplicates in Sorted Linked List


1.Problem Understanding

Given problem statement is sorting a linked list and to remove the duplicate element so that the element appers only once.
Example:
2 → 2 → 4 → 5
Output:
2 → 4 → 5
2.Approach
Start from head and move forward.
At each node:
If current.data == current.next.data
→ skip the next node
Else → move forward
Example:
2 → 2 → 2 → 4 → 5
Step-by-step:
2 == 2 → remove next
2 == 2 → remove next
2 != 4 → move forward
Final:
2 → 4 → 5
3.Algorithm
Start from head
While current and current.next exist:
If same → remove duplicate
Else → move forward
Return head

Top comments (0)