Title: Missing numbers
Given a sorted linked list, remove all duplicate elements such that each element appears only once.Copy the code
Example:
Input: 1 - > 2 - > 1 output: 1 - > 2 input: 1 - > 1 - > 2 - > 3 - > 3 output: 1 - > 2 - > 3Copy the code
Think about:
Loop list nodes, because they are sorted lists, so you only need to compare each element with the next element, and if it is equal, point the next node to the next node.Copy the code
Implementation:
class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode current = head; while (current ! = null && current.next ! = null) {if (current.next-val == current.val) {// Next = current.next-next; } else { current = current.next; }} return head; }}Copy the code