/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 values so that each value appears only once. Return the head node of the modified linked list, which remains sorted.

Example 1

Input: ListNode(2, ListNode(2, ListNode(4, ListNode(4, ListNode(5)))))

Output: [2,4,5]

Explanation: Duplicates 2 and 4 are removed, only unique values remain.

Example 2

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

Output: [-1,0,1]

Explanation: Duplicate 0 is removed.

Example 3

Input: ListNode(7, ListNode(7, ListNode(7)))

Output: [7]

Explanation: All nodes have the same value, only one remains.

Constraints

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

Case 1

Input: ListNode(10, ListNode(11, ListNode(11, ListNode(12))))

Expected: [10,11,12]

Case 2

Input: ListNode(-5, ListNode(-5, ListNode(-3, ListNode(-3, ListNode(-1)))))

Expected: [-5,-3,-1]

Case 3

Input: ListNode(0)

Expected: [0]

Case 4

Input: None

Expected: []

Case 5

Input: ListNode(1, ListNode(2, ListNode(3, ListNode(3, ListNode(3, ListNode(4))))))

Expected: [1,2,3,4]