移出链表元素
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
/**
* 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 *first = new ListNode(0);
first->next = head;
head = first;
while(head != NULL)
{
while(head->next != NULL && head->next->val == val)
head->next = head->next->next;
head = head->next;
}
return first->next;
}
};