KatInterview

83. Remove Duplicates from Sorted List

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to eliminate duplicate nodes from a sorted singly-linked list, ensuring that each unique value appears only once. Since the list is sorted, all duplicate values will be grouped together consecutively. This property is key to an efficient solution. The modification must be done in-place, and the resulting list should remain sorted.

Solution

Remove Duplicates from Sorted List in Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # Use a pointer to traverse the list, starting from the head.
        current = head

        # Traverse the list as long as there is a current node and a next node.
        while current and current.next:
            # If the current node's value is the same as the next node's value,
            # it's a duplicate.
            if current.val == current.next.val:
                # Skip the duplicate node by pointing current's next to the node after the duplicate.
                # Python's garbage collector will handle the unreferenced node.
                current.next = current.next.next
            else:
                # If values are different, no duplicate here. Move to the next node.
                current = current.next
        
        # Return the head of the modified list.
        return head

Remove Duplicates from Sorted List in Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        // Use a pointer to traverse the list, starting from the head.
        ListNode current = head;

        // Traverse the list as long as there is a current node and a next node.
        while (current != null && current.next != null) {
            // If the current node's value is the same as the next node's value,
            // it's a duplicate.
            if (current.val == current.next.val) {
                // Skip the duplicate node by pointing current's next to the node after the duplicate.
                // The Java garbage collector will automatically handle the unreferenced node.
                current.next = current.next.next;
            } else {
                // If values are different, no duplicate here. Move to the next node.
                current = current.next;
            }
        }

        // Return the head of the modified list.
        return head;
    }
}

Remove Duplicates from Sorted List in C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        // Use a pointer to traverse the list, starting from the head.
        ListNode* current = head;

        // Traverse the list as long as there is a current node and a next node.
        while (current != nullptr && current->next != nullptr) {
            // If the current node's value is the same as the next node's value,
            // it's a duplicate.
            if (current->val == current->next->val) {
                // Skip the duplicate node by pointing current's next to the node after the duplicate.
                // In C++, the duplicate node would ideally be deleted to free memory,
                // but on LeetCode, simple reassignment is sufficient as the platform manages memory.
                current->next = current->next->next;
            } else {
                // If values are different, no duplicate here. Move to the next node.
                current = current->next;
            }
        }

        // Return the head of the modified list.
        return head;
    }
};

Remove Duplicates from Sorted List in TypeScript

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function deleteDuplicates(head: ListNode | null): ListNode | null {
    // Use a pointer to traverse the list, starting from the head.
    let current = head;

    // Traverse the list as long as there is a current node and a next node.
    while (current && current.next) {
        // If the current node's value is the same as the next node's value,
        // it's a duplicate.
        if (current.val === current.next.val) {
            // Skip the duplicate node by pointing current's next to the node after the duplicate.
            // The garbage collector will handle the unreferenced node.
            current.next = current.next.next;
        } else {
            // If values are different, no duplicate here. Move to the next node.
            current = current.next;
        }
    }

    // Return the head of the modified list.
    return head;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.