203. Remove Linked List Elements

class Solution { public: ListNode* removeElements(ListNode* head, int val) { if (head == nullptr) return head; head->next = removeElements(head->next, val); return (head->val == val) ? head->next : head; } };
Version 4 : Even easier. Make use of the fact that we change the node after head first, so we do not mess with the links.

Be the first to 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.