+关注继续查看

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

C++实现代码：

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
return NULL;
while(p)
{
if(p->val==val)
{
{
p->next=NULL;
delete p;
}
else
{
pre->next=p->next;
p->next=NULL;
delete p;
p=pre->next;
}
}
else
{
pre=p;
p=p->next;
}
}
}
};

LeetCode 203. Remove Linked List Elements

0 0
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 –>
770 0

597 0
Remove Element
Remove Element删掉指定的元素，并用后面的元素顶替空出来的位置；Remove ElementGiven an array and a value, remove all instances of that value in place and return the new length.
393 0
leetcode 203 Remove Linked List Elements
﻿﻿ Remove all elements from a linked list of integers that have valueval. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 ...
621 0
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
881 0
+关注