83. Remove Duplicates from Sorted List - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

You’re given the head of a sorted singly linked list. Remove all duplicates in place so that each element appears only once, and return the head of the updated list.

Examples

Example 1

Input   head = [1,1,2]  
Output  [1,2]  
Explanation  You start with 1→1→2. You remove the second `1` so it becomes 1→2.

Example 2

Input   head = [1,1,2,3,3]  
Output  [1,2,3]  
Explanation  Duplicates of `1` and `3` are removed.

Example 3

Input   head = [1,1,1,1]  
Output  [1]  
Explanation  All extra `1`s are removed, leaving a single node.

Constraints

  • The number of nodes is in [0, 300]
  • -100 ≤ Node.val ≤ 100
  • The list is sorted in non‑decreasing order

Hints

  1. How can you traverse the list so that when you see a run of equal values you skip over the extras?
  2. What pointers do you need to remember as you unlink duplicate nodes?

Approach 1 using Auxiliary Storage

Idea

Build a new list (or array) of unique values by scanning the original. Then reconstruct a linked list from those values.

Steps

  1. Traverse the original list, appending each new value to a Python list (or Java List<Integer>) only when it differs from the last added.
  2. Create a fresh linked list from that unique‑value list.
  3. Return its head.

Code (Python)

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Approach 2 In‑Place One‑Pass

Idea

Because the list is sorted, all duplicates are consecutive. Use two pointers:

  • current scans node by node.
  • When current.next.val == current.val, unlink current.next.
  • Otherwise advance current.

Steps

  1. If head is null, return it.
  2. Let current = head.
  3. While current.next exists:
    • If current.next.val == current.val, do current.next = current.next.next (skip duplicate).
    • Else current = current.next.
  4. Return head.

Code (Python)

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time O(n) since each node is visited at most once.
  • Space O(1) extra for the in‑place approach (≈O(n) if you rebuild with extra storage).

Step‑by‑Step Walkthrough

Consider 1→1→2→3→3 with the in‑place approach:

  1. curr at first 1. curr.next.val also 1 → unlink second 1. List becomes 1→2→3→3.
  2. curr still at 1. Now curr.next.val is 2 → move curr to node 2.
  3. At 2, curr.next.val is 3 → move curr to 3.
  4. At first 3, curr.next.val is 3 → unlink second 3. List becomes 1→2→3.
  5. curr.next is now null → done.

Common Mistakes

  • Forgetting to check curr for null before accessing curr.next.
  • Skipping the else curr = curr.next step, causing infinite loop on duplicates.
  • Using a fresh head pointer in recursion (overkill here).

Edge Cases

  • Empty list head = null.
  • Single‑node list.
  • No duplicates at all.
  • All nodes identical.

Alternative Variations

  • Remove duplicates II (LeetCode 82): delete all nodes that have duplicate numbers, leaving only distinct values.
  • Allow each value at most k times (e.g. LeetCode 80 for arrays).
  • Remove duplicates from unsorted list (requires hashing).
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;