LeetCode【203】Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

很简单吧,没什么好说的。直接上AC代码:

ListNode* removeElements(ListNode* head, int val) {
        if(!head)
            return NULL;
        ListNode* h=head;
        while(h->val==val)
        {
            if(h->next==NULL)
                return NULL;
            h = h->next;
        }
        
        ListNode* tmp1=h;
        ListNode* tmp2=h->next;
        while(tmp2)
        {
            if(tmp2->val == val)
            {
                tmp2= tmp2->next;
                tmp1->next = tmp2;
            }
            else
            {
                tmp1=tmp1->next;
                tmp2=tmp2->next;
            }
        }
        return h;
    }
原文地址:https://www.cnblogs.com/ww-jin/p/4450370.html