leetcode203. 移除鏈表元素
阿新 • • 發佈:2018-12-06
鏈表 urn amp move head free 元素 link element
刪除鏈表中等於給定值 val 的所有節點。
示例:
輸入: 1->2->6->3->4->5->6, val = 6 輸出: 1->2->3->4->5
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeElements(struct ListNode* head, int val) { while(head && head->val == val) { struct ListNode* tmp = head; head = head->next; free(tmp); } if(head == NULL) return head; struct ListNode* cur = head; while(cur->next) { if(cur->next->val == val) { structListNode* tmp = cur->next; cur->next = cur->next->next; free(tmp); } else { cur = cur->next; } } return head; }
leetcode203. 移除鏈表元素