码迷,mamicode.com
首页 > 其他好文 > 详细

203. Remove Linked List Elements

时间:2020-04-05 13:36:39      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:move   linked   NPU   lin   nod   value   inpu   head   return   

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

标签:move   linked   NPU   lin   nod   value   inpu   head   return   

原文地址:https://www.cnblogs.com/dysjtu1995/p/12636808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!