2022年6月13日 星期一

[83] Remove Duplicates from Sorted List

唉,我愛easy easy 愛我啊T.T
刪掉重覆的linked list node
其實可以寫的非常短!
不過就完整步驟還是應該要好好的delete or 設成NULL 才對(吧)
/**
 * 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->next != NULL)
    {
        struct ListNode* pNext=p->next;
        if (p->val == pNext->val)
        {
            p->next = pNext->next;
//            pNext->next = NULL;
//            pNext=p->next;
        }
        else
        {
//            pNext=p->next;
            p=pNext;
        }        
    }
    return head;
}

沒有留言:

張貼留言