203. Remove Linked List Elements
code
/**
* 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* cur = head, *pre;//
while(cur)
{
if(cur->val == val)
{
if(cur==head) { head = head->next; delete cur; cur = head; }//
else {pre->next = cur->next; delete cur; cur = pre->next;}//
}
else
{
pre = cur;//
cur = cur->next;
}
}
return head;
}
};
View Code
1. Leetcode_remove-linked-list-elements;