↓Skip to main content
  1. LeetCode/

LeetCode 83: Remove Duplicates from Sorted List

·2 mins· ·
LeetCode Easy Linked-List
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-07-20

  • Submissions: Runtime Error ×2 → Time Limit Exceeded ×2 → Accepted → Runtime Error → Wrong Answer ×2 → Accepted → Runtime Error ×2 → Accepted ×4 → Runtime Error → Accepted ×2

Approach
#

In a sorted list duplicates are adjacent. When cur.next equals cur, skip it and keep cur (the next may match too); advance only otherwise (Linked List Template 1). The head always stays, so no dummy is needed.

My Attempts
#

This was one of my first problems in July 2022, and most of the 18 submissions came from not being used to ListNode yet:

  • At first I used self.val, compared a value with a node (head.val != head.next), and returned a list instead of a node: RE and TLE.
  • Later, an empty list returned ListNode(None) or ListNode(0), adding a node (WA), or cur.next went unchecked and [3, 3] raised.

Solution
#

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        cur = head
        while cur and cur.next:
            if cur.val == cur.next.val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return head

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • An empty list returns None, not ListNode(None) or ListNode(0).
  • Compare cur.val with cur.next.val, not a value with a node.

Interview Explanation
#

The list is sorted, so duplicates sit next to each other: skip cur.next when it matches cur, advance otherwise. Time O(n), space O(1).

Related

LeetCode 144: Binary Tree Preorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 144 note: iterative pre-order traversal records on pop and pushes the right child before the left so the left is handled first
LeetCode 94: Binary Tree Inorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 94 note: iterative in-order traversal, pushing nodes while going left, recording on pop, then turning to the right subtree
LeetCode 69: Sqrt(x)
·1 min
LeetCode Easy Math Binary-Search Newtons-Method
LeetCode 69 note: find the largest mid with mid² <= x; when a closed-interval search ends, r is the answer
LeetCode 27: Remove Element
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 27 note: the write pointer copies only values not equal to val, returning how many were written
LeetCode 28: Find the Index of the First Occurrence in a String
·1 min
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 note: try matching needle at every start, or use str.find; index raises when not found
LeetCode 35: Search Insert Position
·2 mins
LeetCode Easy Array Binary-Search
LeetCode 35 note: find the first position >= target (lower bound); a half-open [l, r) needs r = mid, not r = mid - 1