Remove Duplicates from a Sorted Linked List
Introduction When working with linked lists, handling duplicates is a common task. This problem becomes easier when the list is already sorted, because duplicate values will always appear next to e...

Source: DEV Community
Introduction When working with linked lists, handling duplicates is a common task. This problem becomes easier when the list is already sorted, because duplicate values will always appear next to each other. Problem Statement Given a sorted singly linked list, remove all duplicate nodes so that each element appears only once. Return the modified linked list. Example Input: 2 → 2 → 4 → 5 Output: 2 → 4 → 5 Key Insight Since the list is sorted: Duplicate elements will be adjacent So we only need to compare the current node with the next node Approach We traverse the linked list and: If current value == next value → skip the next node Else → move forward Steps Start from head While current and current.next exist: If values are same → remove duplicate Else → move to next node Python Implementation ```python id="4nq7u9" class ListNode: def init(self, val=0, next=None): self.val = val self.next = next def remove_duplicates(head): current = head while current and current.next: if current.val =