Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
AC代码:
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if( head == null || head.next == null )
return head;
//delete duplicates or move on
ListNode curr = head;
ListNode next = curr.next;
while( next != null ){
ListNode temp = next.next;
if( curr.val == next.val ){
curr.next = temp;
next = temp;
continue;
}
curr = curr.next;
next = next.next;
}
return head;
}
}