1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
class Solution { public:
ListNode* deleteDuplicates(ListNode* head) { ListNode* pre = new ListNode(-100000); ListNode* nHead = pre, *tail = pre; int preCnt = 2; while (head) { if (head->val == pre->val) { preCnt++; } else { if (preCnt == 1) { tail->next = pre; tail = pre; tail->next = NULL; } pre = head; preCnt = 1; } head = head->next; }
if (preCnt == 1) { tail->next = pre; pre->next = NULL; }
return nHead->next; } };
|