/**
* 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
最新推荐文章于 2024-11-04 16:26:24 发布