注意这里通过比较node而非值,判断了pre 指针和cur 指针的位置,从而穿针引线
ref http://blog.csdn.net/linhuanmars/article/details/24389429
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null) return head;
ListNode t = new ListNode(-1);
t.next = head;
ListNode pre = t;
ListNode cur = head;
while(cur!=null){
//while(cur.next!=null && pre.next.val==cur.val){
while(cur.next!=null && pre.next.val==cur.next.val){
cur = cur.next;
}
if(pre.next ==cur ) { // 注意这里比较的是node不是val
pre= pre.next;
}
else pre.next = cur.next; //找到不一样的或者终点, 就吧pre穿过去
cur = cur.next;
}
return t.next;
}
}