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;

        
    }
};
相关推荐
Lsk_Smion1 天前
力扣实训 _ [102].层序遍历--前序--后续_递归与非递归的实现
数据结构·算法·leetcode
Lsk_Smion1 天前
力扣实训 _ [25].K个一组链表
数据结构·链表
小欣加油1 天前
leetcode3751 范围内总波动值I
java·数据结构·c++·算法·leetcode
V搜xhliang02461 天前
临床科研新范式:从选题到投稿,AI智能体如何接管全流程?
运维·数据结构·人工智能·算法·microsoft·数据挖掘·自动化
梦想的颜色1 天前
MySQL 查询性能核武器
运维·服务器·数据结构·数据库·mysql
八解毒剂1 天前
查找-从二分查找到二叉排序树
数据结构·c++·算法
_日拱一卒1 天前
LeetCode:78子集
数据结构·算法·leetcode·职场和发展
FuckPatience1 天前
C# new List<T>(IEnumerable<T> collection),链表初始化时传入已存在链表
链表·c#·list
東隅已逝,桑榆非晚1 天前
数据结构:算法效率与复杂度分析详解
数据结构·笔记·算法
半夜修仙1 天前
分治思想对数组进行排序-归并排序
数据结构·算法·排序算法