bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 82. Remove Duplicates from Sorted List II

82. Remove Duplicates from Sorted List II

Leetcode Solutions

Sentinel Head + Predecessor

  1. Initialize a sentinel node with a value that is not in the list (e.g., 0) and set it to point to the head of the list.
  2. Initialize two pointers: predecessor to point to the sentinel node and current to point to the head of the list.
  3. Iterate through the list while current is not null.
  4. While current.next is not null and current.val is equal to current.next.val, keep moving current forward to skip over duplicates.
  5. If current has moved (indicating duplicates were found), set predecessor.next to current.next to skip the entire duplicate sublist. Otherwise, move predecessor forward.
  6. Move current forward to the next node.
  7. After the iteration, return sentinel.next as the new head of the modified list.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...