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;

        
    }
};
相关推荐
风落青芜11 分钟前
C++入门:类和对象(三)
开发语言·数据结构·c++·算法
Pniubi1 小时前
力扣25.K个一组反转链表
算法·leetcode·链表
杜 硕2 小时前
从零开始的C++生活——第十五讲
开发语言·数据结构·c++·生活
小白的高手之路2 小时前
【LeetCode 热题 100】560. 和为 K 的子数组|前缀和、哈希表(Python)
数据结构·笔记·python·算法·leetcode·力扣·散列表
蓝色汪洋3 小时前
qsort使用指南
数据结构·算法
Code_Solitude5 小时前
c语言:期末复习5(二维数组)
c语言·数据结构·算法
charliejohn5 小时前
计算机考研 408 数据结构 散列表 平均查找长度 ASL
数据结构·散列表
charliejohn5 小时前
计算机考研 408 数据结构 图的概念及例题
数据结构
Shaoshing8 小时前
LeetCode100 32.最长有效括号
java·数据结构·leetcode
Lazyafei8 小时前
560. 和为 K 的子数组
数据结构·算法