class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* Head=new ListNode(-1);
Head->next=head;
ListNode* cur=head,*pre=Head;
while(cur)
{
if(cur->val==val)
{
pre->next=cur->next;
delete cur;
cur=pre->next;
}
else
{
pre=cur;
cur=cur->next;
}
}
head=Head->next;
delete Head;
return head;
}
}; 203. Remove Linked List Elements
最新推荐文章于 2024-11-04 16:26:24 发布
本文介绍了一种从链表中移除特定值元素的方法。通过使用虚拟头节点简化边界条件处理,遍历链表并删除所有等于指定值的节点。提供了完整的C++实现代码。
740

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



