Remove Duplicates from Sorted Li
2017-12-26 本文已影响0人
无云清晨
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head)
{
if(NULL == head)
return NULL;
struct ListNode* p = head;
struct ListNode* q = head->next;
int val1 = p->val;
int val2;
while(q != NULL)
{
val2 = q->val;
if(val2 == val1)
{
q = q -> next;
if(q == NULL)
{
p->next = q;
}
}
else
{
val1 = val2;
p ->next = q;
p = q;
q = q -> next;
}
}
return head;
}