1、用一个指针circu指向头结点,用circu去遍历链表,当circu->next的val值为val时,则删掉circu的next结点。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
while(head!=NULL&& head->val==val) head=head->next;
ListNode* circu=head;
while(circu!=NULL)
{
if(circu->next!=NULL&&circu->next->val==val) circu->next=circu->next->next;
else circu=circu->next;
}
return head;
}
};