代码随想录链接
我的解法:
/*** 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 != nullptr && head->val == val){head = head->next;}if(head==nullptr) return head;//第一个节点不等于valListNode * left = head;ListNode * right = head->next;while(right != nullptr){if(right->val == val){ //删除节点left->next = right->next;right = right->next;}else{left = right;right = right->next;}}return head;}
};