Loading...
Loading...
You are given head, the first node of a singly linked list whose values are sorted in ascending order.
Remove every node whose value appears more than once in the list, so that only the values that appear exactly once remain. The remaining nodes keep their original order.
Return the head of the resulting list.
Your function receives head as a ListNode (or null for the empty list) and returns the head of the resulting list (null if no node remains). The node type is provided for you, with val and next fields; you may relink the existing nodes.
The examples below write a list as the array of its values in order, so [1,2,3] is the list 1 -> 2 -> 3. That is only how lists are displayed; the encoding and decoding are done for you.
Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
Explanation: 3 and 4 each appear twice, so all four of those nodes are removed. 1, 2, and 5 appear once and stay.
Input: head = [1,1,1,2,3]
Output: [2,3]
Explanation: 1 appears three times, so all three nodes with value 1 are removed.
Input: head = [7,7]
Output: []
Explanation: Both nodes hold the repeated value 7, so the resulting list is empty.
Node.val ≤100