Short 36ms C++ solution


  • -1
    D
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            while (head && head->val == val) { head = head->next; }
            if (!head) { return nullptr; }
            
            auto curr = head;
            while (curr->next) {
                if (curr->next->val == val) {
                    curr->next = curr->next->next;
                } else {
                    curr = curr->next;
                }
            }
            return head;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.