/**
* 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) {
ListNode* dummyhead = new ListNode();
dummyhead->next = head;
ListNode* cur = dummyhead;
while(cur->next != nullptr) {
if(cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next = tmp->next;
delete tmp;
} else {
cur = cur->next;
}
}
ListNode* result = dummyhead->next;
delete dummyhead;
return result;
}
};
力扣203. 移除链表元素
最新推荐文章于 2025-11-24 14:44:53 发布
973

被折叠的 条评论
为什么被折叠?



