/83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List

Easy
Linked Lists56.4% acceptance

Given the head node of a singly linked list sorted in ascending order, remove all duplicate nodes so that each value appears only once. Return the head node of the modified linked list, which remains sorted.

Example 1

Input: ListNode(0, ListNode(0, ListNode(1, ListNode(2, ListNode(2)))))

Output: [0,1,2]

Explanation: Duplicates 0 and 2 are removed.

Example 2

Input: ListNode(-2, ListNode(-1, ListNode(-1, ListNode(0, ListNode(1)))))

Output: [-2,-1,0,1]

Explanation: Duplicate -1 is removed.

Example 3

Input: ListNode(5, ListNode(5, ListNode(5, ListNode(5))))

Output: [5]

Explanation: All nodes are duplicates except the first.

Constraints

  • 0 <= number of nodes <= 300
  • -100 <= node value <= 100
  • Input list is sorted in ascending order
Python (current runtime)

Case 1

Input: ListNode(3, ListNode(4, ListNode(4, ListNode(5, ListNode(5, ListNode(6))))))

Expected: [3,4,5,6]

Case 2

Input: ListNode(-10, ListNode(-10, ListNode(-5, ListNode(-5, ListNode(0)))))

Expected: [-10,-5,0]

Case 3

Input: ListNode(7)

Expected: [7]

Case 4

Input: None

Expected: []