/**
* 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) return NULL;
ListNode *ret = head;
ListNode *pre = NULL;
ListNode *cur = NULL;
while (ret && ret->val == val) {
ret = ret->next;
}
if (!ret) return NULL;
pre = ret;
cur = ret->next;
while (cur) {
if (cur->val == val) {
pre->next = cur->next;
cur = cur->next;
} else {
pre = pre->next;
cur = cur->next;
}
}
return ret;
}
};Remove Linked List Elements
最新推荐文章于 2024-11-04 16:26:24 发布
156

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



