class Solution {
public:
/**
* @param head a ListNode
* @param val an integer
* @return a ListNode
*/
ListNode *removeElements(ListNode *head, int val) {
ListNode *first=head;
if(head==NULL)
return head;
while((head->next)!=NULL)
{
if(head->next->val==val)
{
if(head->next->next==NULL)
{
head->next=NULL;
break;
}
else
{
head->next=head->next->next;
}
}
else
head=head->next;
}
if(first->val==val)
return first->next;
else
return first;
}
水题 。。
删除链表中的元素 - C++
最新推荐文章于 2024-07-27 09:11:29 发布