题意:删除链表中值为给定值的元素。
思路:简单模拟。
/**
* 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 *next = head;
while(head && head->val == val) {
head = head->next;
}
next = head;
while(next && next->next) {
if(next->next->val == val) {
next->next = next->next->next;
}
else next = next->next;
}
return head;
}
};