Remove Duplicates from Sorted List LeetCode Solution

Last updated on January 29th, 2025 at 02:21 am

Here, we see a Remove Duplicates from Sorted List LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

List of all LeetCode Solution

Topics

Linked List

Level of Question

Easy

Remove Duplicates from Sorted List LeetCode Solution

Remove Duplicates from Sorted List LeetCode Solution

1. Problem Statement

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

Input: head = [1,1,2]
Output: [1,2]

Example 2:

Input: head = [1,1,2,3,3]
Output: [1,2,3]

2. Coding Pattern Used in Solution

The coding pattern used in all the provided implementations is “Two Pointers”. This pattern is evident because the code iterates through the linked list using a single pointer (tmpcurrent, or curr), and it modifies the next pointer of the current node to skip over duplicate nodes. This approach effectively uses two pointers: one to track the current node and another implicitly to track the next node being compared.

3. Code Implementation in Different Languages

3.1 Remove Duplicates from Sorted List C++

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
	if(!head) return head;
	ListNode* tmp = head;
	while(tmp && tmp -> next)
	{
		if(tmp -> val == tmp -> next -> val)
			tmp -> next = tmp -> next -> next;
		else
			tmp = tmp -> next; 
	}
	return head;       
    }
};

3.2 Remove Duplicates from Sorted List Java

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)return head;
        head.next = deleteDuplicates(head.next);
        return head.val == head.next.val ? head.next : head;        
    }
}

3.3 Remove Duplicates from Sorted List JavaScript

var deleteDuplicates = function(head) {
    var current = head;
    while(current) {
        if(current.next !== null && current.val == current.next.val) {
            current.next = current.next.next;
        } else {
            current = current.next;
        }
    } 
    return head;    
};

3.4 Remove Duplicates from Sorted List Python

class Solution(object):
    def deleteDuplicates(self, head):
        if head == None:
            return head
        curr = head
        while curr.next != None:
            if curr.val == curr.next.val:
                tmp = curr.next
                curr.next = curr.next.next
                del tmp
            else:
                curr = curr.next
        return head   

4. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(n)O(1)
JavaO(n)O(n)
JavaScriptO(n)O(1)
PythonO(n)O(n)
  • The code efficiently removes duplicates from a sorted linked list by iterating through it and modifying the next pointers.
  • The time complexity is O(n) for all implementations.
  • The space complexity is O(1) for C++, JavaScript, and Python, but O(n) for Java due to recursion.
Scroll to Top