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;

        
    }
};
相关推荐
如竟没有火炬2 小时前
接雨水22
数据结构·python·算法·leetcode·散列表
ʚ希希ɞ ྀ2 小时前
二叉树的锯齿层序遍历
数据结构·算法
tyung4 小时前
用 Go 实现一个生产级 Ring Buffer Queue:环形数组、位运算取模、批量操作全拆解
数据结构·go
SHARK_pssm6 小时前
【数据结构——复杂度】
c语言·数据结构·经验分享·笔记
故事和你916 小时前
洛谷-【图论2-1】树2
开发语言·数据结构·c++·算法·动态规划·图论
努力努力再努力wz7 小时前
【Qt入门系列】深入理解信号与槽:从事件响应到自定义信号机制
c语言·开发语言·数据结构·数据库·c++·qt·mysql
Ricky_Theseus7 小时前
B树和B+树的区别
数据结构·b树
爱炼丹的James8 小时前
第二章 数据结构
数据结构
我爱cope8 小时前
【前缀和:3. 无重复字符的最长子串】
数据结构·算法·leetcode
不知名的忻8 小时前
关键路径(Java)
java·数据结构·算法·关键路径