首页 > 代码库 > LeetCode "Remove Duplicates from Sorted List II"
LeetCode "Remove Duplicates from Sorted List II"
Corner cases!
class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if (!head) return head; if (!head->next) return head; ListNode dum(-1); dum.next = head; ListNode *pPre = &dum; ListNode *p1 = head; int cnt = 0; int last = std::numeric_limits<int>::max(); while (p1) { // A new one? if (p1->val != last) { if (cnt > 1) { pPre->next = p1; last = p1->val; } else { last = p1->val; while (pPre->next != p1) pPre = pPre->next; } cnt = 1; } else { cnt++; } p1 = p1->next; } if (cnt > 1) { if (pPre == &dum) return NULL; else { pPre->next = NULL; } }; return dum.next; }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。