Skip to content

Latest commit

 

History

History
35 lines (31 loc) · 798 Bytes

203-remove-linked-list-elements.md

File metadata and controls

35 lines (31 loc) · 798 Bytes
title tags grammar_cjkRuby
203-remove-linked-list-elements
在河之洲,算法,小书匠
true

problem

203-remove-linked-list-elements

solution

    ListNode* removeElements(ListNode* head, int val) {
        if (head == NULL)
            return NULL;
        ListNode *dummy = new ListNode(0);
        dummy->next = head;
        ListNode *cur = dummy;
        while (cur->next !=NULL)
        {
            if (cur->next->val == val)
            {
                ListNode *tmp = cur->next;
                cur->next = tmp->next;
                delete tmp;
            }
            else
            {
                cur = cur->next;
            }
        }
        return dummy->next;
    }