C++ 28ms beats 100.00% of cppsubmissions


  • 0
    B
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if(head == NULL)
                return NULL;
            while(head && head->val == val)
                head = head->next;
            if(head == NULL) return head;
            else {
                ListNode *ptr = head;
                while(ptr->next) {
                    if(ptr->next->val == val)
                        ptr->next = ptr->next->next;
                    else
                        ptr = ptr->next;
                }
            }
            return head;
        }
    };

Log in to reply
 

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