/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head==NULL)return NULL;
while(head!=NULL&&head->val==val)
{
head=head->next;
}
if(head==NULL)return NULL;
ListNode *p=head;
ListNode *last=p;
while(p->next!=NULL)
{
p=p->next;
if(p->val==val)
{
last->next=p->next;
}
else
last=p;
}
if(p->val==val&&p->next==NULL)
{
last->next=NULL;
}
return head;
}
};
leetcode 203: Remove Linked List Elements
最新推荐文章于 2022-12-25 18:05:51 发布