题目描述
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
题解
添加冗余的头节点!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ListNode* deleteDuplicates(ListNode* head) { if (!head||!head->next)return head; ListNode*prehead; prehead = new ListNode(-1); prehead->next = head; ListNode* cur = prehead; while (cur->next&&cur->next->next) { if (cur->next->val == cur->next->next->val) { ListNode*tmp = cur->next; while (tmp->next&&tmp->val == tmp->next->val) tmp = tmp->next; cur->next = tmp->next; } else cur = cur->next; } return prehead->next; }
|