leetcode系列(76)Remove Linked List Elements

来源:互联网 发布:dw淘宝代码使用教程 编辑:程序博客网 时间:2024/06/06 10:49

Remove all elements from a linked list of integers that havevalue val.

Example
Given:
 1 --> 2 --> 6 --> 3 --> 4 --> 5 -->6, val = 6

Return: 1 --> 2 --> 3 --> 4 --> 5

class Solution {public:    ListNode* removeElements(ListNode* head, int val) {        if (head == nullptr) {            return nullptr;        }        if (head->val == val) {            auto ptr = head->next;            delete head;            head = removeElements(ptr, val);        } else {            head->next = removeElements(head->next, val);        }        return head;    }};


0 0