文章来源:https://www.toymoban.com/news/detail-737274.html
/**
* 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;
}
};
文章来源地址https://www.toymoban.com/news/detail-737274.html
到了这里,关于LEEDCODE 203移除链表元素的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!