0203.cpp (372B)
1 class Solution { 2 public: 3 ListNode *removeElements(ListNode *head, int val) { 4 if (!head) return nullptr; 5 6 for (ListNode *p = head; p && p->next;) 7 if (p->next->val == val) 8 p->next = p->next->next; 9 else 10 p = p->next; 11 12 if (head->val == val) head = head->next; 13 14 return head; 15 } 16 };