题解 | 移除链表元素
移除链表元素
https://www.nowcoder.com/practice/428a854dff8b4333b54cfe580323e2df
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
ListNode* removeElements(ListNode* head, int val) {
// write code here
auto dummy=new ListNode(0);
dummy->next=head;
auto cur=dummy;
while(cur->next!=nullptr){
if(cur->next->val==val){
auto temp=cur->next;
cur->next=temp->next;
delete temp;
}
else {cur=cur->next;}
}
head=dummy->next;
delete dummy;
return head;
}
};//终于知道了为什么总是编译错误了,就是少了一个分号,我还看了半天代码
