A 32ms Simple C++ Solution


  • 0
    Y
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            ListNode* curr = head;
            ListNode root(0);
            root.next = head;
            head = &root;
            
            while(curr) {
                if(curr->val == val) {
                    head->next = curr->next;
                    curr = curr->next;
                } else {
                    head = curr;
                    curr = curr->next;
                }
            }
            return root.next;
        }
    };

Log in to reply
 

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