203. Remove Linked List Elements

Posted dysjtu1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了203. Remove Linked List Elements相关的知识,希望对你有一定的参考价值。

Problem:

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

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

思路

Solution (C++):

ListNode* removeElements(ListNode* head, int val) {
    ListNode dummyNode(0);
    using L = ListNode*;
    L dummy = &dummyNode, pre = dummy, cur = head;
    dummy->next = head;
    while (cur) {
        while (cur && cur->val == val) {
            cur = cur->next;
            pre->next = cur;
        }
        if (cur) {
            pre = cur;
            cur = cur->next;                
        }
    }
    return dummy->next;
}

性能

Runtime: 44 ms??Memory Usage: 10.8 MB

思路

Solution (C++):


性能

Runtime: ms??Memory Usage: MB

以上是关于203. Remove Linked List Elements的主要内容,如果未能解决你的问题,请参考以下文章

203. Remove Linked List Elementseasy

#Leetcode# 203. Remove Linked List Elements

203. Remove Linked List Elements

203. Remove Linked List Elements

203. Remove Linked List Elements [easy] (Python)

[LeetCode]203. Remove Linked List Elements