203. Remove Linked List Elements

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ // This node has some memory leak class Solution { public: ListNode* removeElements(ListNode* head, int val) { // zero node if (head == nullptr) return head; // one node if (head->next == nullptr){ if (head->val == val){ head = nullptr; } return head; } // >= 2 nodes ListNode * curr = head; while(curr->next-> != nullptr){ if (curr->next->val == val){ curr->next = curr->next->next; } curr = curr->next; } return head; } };
Version 1

1 Response

Line 29: member access within null pointer of type 'struct ListNode'

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.