效果:
0->1->1->2->3->null
输出
0->1->2->3->null
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: head node
*/
ListNode *deleteDuplicates(ListNode *head) {
// write your code here
if(head==NULL) return NULL;
ListNode *p=head;
while(p->next){
if(p->next->val==p->val){
ListNode *q=p->next;
p->next=q->next;
q->next=NULL;
delete q;
}
else p=p->next;
}
return head;
}
};