203. Remove Linked List Elements

风流意气都作罢 提交于 2020-04-05 15:31:33

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!