public class Solution {
public ListNode deleteDuplicates(ListNode head) {
int c;
ListNode newhead=head;
ListNode last=head;
boolean needMoveHead=true;
while(head!=null&&head.next!=null){
c=0;
while(head.next!=null&&head.val==head.next.val){
c++;
head=head.next;
}
if(c>=1){
// need to cut it
if(needMoveHead){
// it's the first element, move the new head
newhead=head.next;
}
// cut it
last.next=head.next;
}else{
// doesn't need to cut it. No need to move new head now
needMoveHead=false;
last=head;
}
head=head.next;
}
return newhead;
}
}