LEEDCODE 203移除链表元素

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(head == nullptr)
        {
            return nullptr;
        }
        ListNode* _dummyhead = new ListNode(0);
        _dummyhead->next = head;

        ListNode* pre = _dummyhead;
        ListNode* cur = _dummyhead->next;
        while(cur)
        {
            if(cur->val == val)
            {
                ListNode* p = cur;
                pre->next = cur->next;
                cur = cur->next;
                delete p;

            }
            else
            {
                cur = cur->next;
                pre = pre->next;

            }
        }
        return _dummyhead->next;

        
    }
};
相关推荐
游乐码14 小时前
C#Queue
数据结构·游戏·c#
汀、人工智能15 小时前
[特殊字符] 第67课:跳跃游戏II
数据结构·算法·数据库架构·图论·bfs·跳跃游戏ii
手握风云-16 小时前
优选算法的层序之径:队列专题
数据结构·算法·leetcode
比昨天多敲两行16 小时前
C++ 哈希表
数据结构·哈希算法·散列表
历程里程碑16 小时前
Protobuf总结
大数据·数据结构·elasticsearch·链表·搜索引擎
lg_cool_16 小时前
Python 框架之py_trees
开发语言·数据结构·python
曹牧16 小时前
svn: svn relocate ‌之externals‌
数据结构·svn
北顾笙98017 小时前
day20-数据结构力扣
数据结构·算法·leetcode
深邃-17 小时前
【C语言】-数据在内存中的存储(2):浮点数在内存中的存储
c语言·开发语言·数据结构·c++·算法·html5