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; }
【本文章出自博客园willaty,转载请注明作者出处,误差欢迎指出~】