Remove all elements from a linked list of integers that have value val.

Example

Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6

Return: 1 –> 2 –> 3 –> 4 –> 5

思路很简单,但是注意移动指针的条件:只有当没有删除节点时,指针才往后移,以后切记要仔细一点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 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 *dummy = new ListNode(0);
dummy->next = head;
ListNode *ptr = dummy;
while (ptr != NULL && ptr->next != NULL) {
if (ptr->next->val == val) {
if (ptr->next->next == NULL)
ptr->next = NULL;
else {
ptr->next->val = ptr->next->next->val;
ptr->next->next = ptr->next->next->next;
}
} else {
ptr = ptr->next;
}
}
return dummy->next;
}
};