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;

        
    }
};
相关推荐
iuu_star4 分钟前
C语言数据结构-顺序查找、折半查找
c语言·数据结构·算法
漫随流水18 分钟前
leetcode算法(515.在每个树行中找最大值)
数据结构·算法·leetcode·二叉树
千金裘换酒2 小时前
LeetCode反转链表
算法·leetcode·链表
一起努力啊~5 小时前
算法刷题--长度最小的子数组
开发语言·数据结构·算法·leetcode
小北方城市网5 小时前
第1课:架构设计核心认知|从0建立架构思维(架构系列入门课)
大数据·网络·数据结构·python·架构·数据库架构
好易学·数据结构6 小时前
可视化图解算法77:零钱兑换(兑换零钱)
数据结构·算法·leetcode·动态规划·力扣·牛客网
独自破碎E6 小时前
【归并】单链表的排序
数据结构·链表
L_09076 小时前
【C++】高阶数据结构 -- 平衡二叉树(AVLTree)
数据结构·c++
冰冰菜的扣jio6 小时前
Redis基础数据结构
数据结构·数据库·redis
Qhumaing7 小时前
C++学习:【PTA】数据结构 7-2 实验6-2(图-邻接表)
数据结构·c++·学习