leetcode 203. Remove Linked List Elements

删除链表中某个元素。

要么直接删,要么值覆盖。算法题就不用free了。

    ListNode* removeElements(ListNode* head, int val) {
        if (head == NULL) return NULL;
        while (head && head->val == val) {
            head = head->next;
        }
        if (!head)
            return NULL;
        
        auto now = head;
        auto last = head;
        while (now) {           
            if (now->next && now->next->val == val) {
                now->next = now->next->next;
                continue;
            }
            now = now->next;
        }
        return head;
    }

 

posted on 2018-01-30 14:13  willaty  阅读(79)  评论(0编辑  收藏  举报

导航