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;

        
    }
};
相关推荐
Queenie_Charlie4 小时前
前缀和的前缀和
数据结构·c++·树状数组
念越8 小时前
数据结构:栈堆
java·开发语言·数据结构
dear_bi_MyOnly8 小时前
【多线程——线程状态与安全】
java·开发语言·数据结构·后端·中间件·java-ee·intellij-idea
浪客灿心9 小时前
list_stack_queue
数据结构·list
zh_xuan9 小时前
最小跳跃次数
数据结构·算法
zh_xuan9 小时前
单青蛙跳台阶
数据结构·算法
罗湖老棍子10 小时前
【 例 1】石子合并(信息学奥赛一本通- P1569)
数据结构·算法·区间dp·区间动态规划·分割合并
小高Baby@11 小时前
JSON、bind、form
数据结构·json
TracyCoder12312 小时前
LeetCode Hot100(21/100)——234. 回文链表
算法·leetcode·链表
数智工坊13 小时前
【数据结构-栈】3.1栈的顺序存储-链式存储
java·开发语言·数据结构