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;

        
    }
};
相关推荐
sonnet-10293 分钟前
堆排序算法
java·c语言·开发语言·数据结构·python·算法·排序算法
handsomethefirst10 分钟前
【算法与数据结构】【面试经典150题】【题41-题45】
数据结构·算法·leetcode
AlenTech13 小时前
141. 环形链表 - 力扣(LeetCode)
数据结构·leetcode·链表
迈巴赫车主14 小时前
蓝桥杯20560逃离高塔
java·开发语言·数据结构·算法·职场和发展·蓝桥杯
x_xbx15 小时前
LeetCode:1. 两数之和
数据结构·算法·leetcode
玲娜贝儿--努力学习买大鸡腿版15 小时前
hot 100 刷题记录(1)
数据结构·python·算法
努力中的编程者16 小时前
二叉树(C语言底层实现)
c语言·开发语言·数据结构·c++·算法
宵时待雨16 小时前
C++笔记归纳14:AVL树
开发语言·数据结构·c++·笔记·算法
山川行17 小时前
关于《项目C语言》专栏的总结
c语言·开发语言·数据结构·vscode·python·算法·visual studio code
Yvonne爱编码17 小时前
Java 中的 hashCode () 与 equals () 核心原理、契约规范、重写实践与面试全解
java·开发语言·数据结构·python·hash