AC代码贴上!
60ms
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL) return NULL;
if(head->next == NULL) return head;
ListNode *p,*q;
p = head;
q = head->next;
while(q != NULL)
{
if(p->val == q->val)
{
q = q->next;
if(q == NULL) //比处理数组要多一个结尾的判断,因为没有长度的返回,结尾有重复的时候,直接让重复的第一个指向NULL即可
{
p->next = NULL;
}
}
else
{
p->next = q;
p = q;
q = q->next;
}
}
return head;
}
};