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;

        
    }
};
相关推荐
0+1115 小时前
算法 --链表
数据结构·算法·leetcode·链表
夜之眷属8 小时前
PriorityQueue:for-each 遍历和 poll 取出,顺序一样吗?
java·服务器·数据结构·后端·算法
思维新观察9 小时前
三星 Galaxy Tab S12 系列:大屏 AI 平板,兼顾轻薄与生产力
数据结构·人工智能·电脑
杜 硕10 小时前
快速排序详解01
数据结构·算法·排序算法
信奥卷王10 小时前
[GESP202606 六级] 满二叉树
数据结构·算法
念越11 小时前
题库、练习与错题本代码阅读
开发语言·数据结构
垆边人似月.12 小时前
华为题:盛最多水的容器
数据结构·算法
HPtiger12 小时前
数据结构——插入排序
数据结构·排序算法
All for pursuit.12 小时前
【动态规划-11】279.完全平方数
数据结构·c++·算法·leetcode·动态规划
Logic10113 小时前
C语言/数据结构位运算题解:异或XOR找出套碗游戏中的“幸运碗“——只出现一次的数字
c语言·数据结构·算法·数组·位运算·时间复杂度·空间复杂度