LeetCode 83 Remove Duplicates from Sorted List

LeetCode 83 Remove Duplicates from Sorted List

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(head==NULL || head->next==NULL)
        return head;
    struct ListNode* p = head;
    while(p&&p->next)
    {
        if(p->val == p->next->val)
        {
            struct ListNode *tmp = p->next;
            p->next=p->next->next;
            free(tmp);
        }
        else p=p->next;
    }
    return head;
}

 

posted @ 2015-11-17 10:24  Walker_Lee  阅读(108)  评论(0编辑  收藏  举报