-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path203.cpp
More file actions
52 lines (46 loc) · 966 Bytes
/
203.cpp
File metadata and controls
52 lines (46 loc) · 966 Bytes
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead=new ListNode(0);
dummyHead->next=head;
//while(head!=NULL&&head->val==val)
//{
// //删除头节点,可能好多头节点都是.空的删不了
// ListNode* deleteNode=head;
// head=deleteNode->next;
// delete deleteNode;
//}
//if(head==NULL)
// return NULL;
ListNode* cur =dummyHead;
while(cur->next !=NULL)
{
if(cur->next->val==val)
{
//删除cur->next;
ListNode* deleteNode=cur->next;
cur->next=deleteNode->next;
delete deleteNode;
}
else cur=cur->next;
}
ListNode* retNode=dummyHead->next;
delete dummyHead;
return retNode;
}
};