/**
* 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) {
ListNode* ans=head,*p;
while(ans && ans->val==val) ans=head->next,delete head,head=ans;
if(!ans) return ans;
while(head->next)
if(head->next->val!=val) head=head->next;
else p=head->next,head->next=p->next,delete p;
return ans;
}
};
Leetcode 203. 删除链表中的节点
最新推荐文章于 2024-09-14 20:09:59 发布