82. Remove Duplicates from Sorted List II LeetCode Solution

In this guide, you will get 82. Remove Duplicates from Sorted List II LeetCode Solution with the best time and space complexity. The solution to Remove Duplicates from Sorted List II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Remove Duplicates from Sorted List II solution in C++
  4. Remove Duplicates from Sorted List II solution in Java
  5. Remove Duplicates from Sorted List II solution in Python
  6. Additional Resources
82. Remove Duplicates from Sorted List II LeetCode Solution image

Problem Statement of Remove Duplicates from Sorted List II

Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

Example 1:

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

Example 2:

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

Constraints:

The number of nodes in the list is in the range [0, 300].
-100 <= Node.val <= 100
The list is guaranteed to be sorted in ascending order.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

82. Remove Duplicates from Sorted List II LeetCode Solution in C++

class Solution {
 public:
  ListNode* deleteDuplicates(ListNode* head) {
    ListNode dummy(0, head);
    ListNode* prev = &dummy;

    while (head != nullptr) {
      while (head->next && head->val == head->next->val)
        head = head->next;
      if (prev->next == head)
        prev = prev->next;
      else
        prev->next = head->next;
      head = head->next;
    }

    return dummy.next;
  }
};
/* code provided by PROGIEZ */

82. Remove Duplicates from Sorted List II LeetCode Solution in Java

class Solution {
  public ListNode deleteDuplicates(ListNode head) {
    ListNode dummy = new ListNode(0, head);
    ListNode prev = dummy;

    while (head != null) {
      while (head.next != null && head.val == head.next.val)
        head = head.next;
      if (prev.next == head)
        prev = prev.next;
      else
        prev.next = head.next;
      head = head.next;
    }

    return dummy.next;
  }
}
// code provided by PROGIEZ

82. Remove Duplicates from Sorted List II LeetCode Solution in Python

class Solution:
  def deleteDuplicates(self, head: ListNode) -> ListNode:
    dummy = ListNode(0, head)
    prev = dummy

    while head:
      while head.next and head.val == head.next.val:
        head = head.next
      if prev.next == head:
        prev = prev.next
      else:
        prev.next = head.next
      head = head.next

    return dummy.next
# code by PROGIEZ

Additional Resources

See also  739. Daily Temperatures LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.