本题用双指针,先定义一个tail使其一直遍历,并与定义的cur进行比较,如果不相等,则将cur的next指向tail,并将cur传至tail位置,如果相等则tail一直遍历。
struct ListNode* deleteDuplicates(struct ListNode* head ) {
 if(head==NULL)
     return head;
    struct ListNode* cur,*tail;
    cur=head;
    tail=cur->next;
    while(tail)   //tail一直遍历
    {        
        if(cur->val==tail->val)
        {
            tail=tail->next;//如果相等则tail一直向后走。            
        }
        else 
        {
            cur->next=tail;//否则改变cur的next指向,与cur的地址
            cur=tail;
            tail=tail->next;
        }
    }
    cur->next=tail;//这段话注意一定要加,当出现111222这样的情况,如果没有加cur的next指向就是1222,因为上面代码当tail为空就跳出来了,cur的next还来不及改变。
    return head;
}