Tensorum
LeetCode 82 · Basics

Remove Duplicates from Sorted List II

LeetCode 82 · Medium · solution, intuition & step-by-step walkthrough

Dummy node; skip whole duplicate runs.

MediumLinked ListTwo Pointers
0 msruntime · beats 100%
19.3 MBmemory · beats 39%
1approach
4accepted runs · last 15 Mar 2026

This is how I solved Remove Duplicates from Sorted List II (LeetCode 82), a medium problem from the Basics pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Every value that appears more than once must vanish completely. Use a dummy head and a tail pointer: when the current node starts a run of duplicates, skip the whole run; otherwise attach it to the result.

Approach

  1. 1

    dummy → temp is the tail of the kept list.

  2. 2

    If node and node.next share a value, advance to the last of the run and link temp past it.

  3. 3

    Otherwise link node and advance temp.

  4. 4

    Move node forward.

How I solved it

Dummy head + run skippingPythonO(n) time · O(1) space
# 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]:
        dummy = ListNode(-1)
        temp = dummy
        node = head
        while node:
            if node.next and node.val == node.next.val:
                while node.next and node.val == node.next.val:
                    node = node.next
                temp.next = node.next
            else:
                temp.next = node
                temp = temp.next
            node = node.next
        return dummy.next
Loading the step-through…

Remove Duplicates from Sorted List II — FAQ

What is the intuition behind Remove Duplicates from Sorted List II?

Every value that appears more than once must vanish completely. Use a dummy head and a tail pointer: when the current node starts a run of duplicates, skip the whole run; otherwise attach it to the result.

How do you solve Remove Duplicates from Sorted List II (LeetCode 82)?

1. dummy → temp is the tail of the kept list. 2. If node and node.next share a value, advance to the last of the run and link temp past it. 3. Otherwise link node and advance temp. 4. Move node forward.

What is the time and space complexity of Remove Duplicates from Sorted List II?

Time: O(n). Space: O(1). (Accepted approach: Dummy head + run skipping.)

What are common mistakes in Remove Duplicates from Sorted List II?

The dummy head handles duplicates at the very start.