Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
含义:如果列表中有重复数字的,把他们全部删除掉
1 public ListNode deleteDuplicates(ListNode head) { 2 if (head == null || head.next == null) return head; 3 ListNode newHead = new ListNode(0); 4 newHead.next = head; 5 ListNode pre = newHead, cur = head; 6 while (cur != null) { 7 while (cur.next != null && cur.val == cur.next.val) cur = cur.next; 8 if (pre.next == cur) pre = cur; 9 else pre.next = cur.next; 10 cur = cur.next; 11 } 12 return newHead.next; 13 }