1. 程式人生 > >203 移除連結串列元素

203 移除連結串列元素

    ListNode *removeElements(ListNode *head, int val) {
        if (head == nullptr)
            return head;
        auto *prehead = new ListNode(-1);
        prehead->next = head;
        head = prehead;
        while (head&&head->next) {
            if (head->next->val == val)
                head->next = head->next->next;
            else
                head = head->next;
        }
        return prehead->next;
    }