/**
* 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* vHead = new ListNode(0); //虚拟头结点
vHead->next = head;
ListNode* pre = vHead;
ListNode* cur = head;
while(cur){
if(cur -> val == val){
pre -> next = cur -> next;
ListNode* tmp = cur;
cur = cur -> next;
delete tmp;
}else {
pre = cur;
cur = cur -> next;
}
}
return vHead -> next;
}
};
<Talk is cheap, show me the code 33> LeetCode.203 移除链表元素
最新推荐文章于 2024-08-04 23:03:31 发布