觉着自己写的比看到的答案精简,分享一下:
class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if(head == NULL) return NULL; ListNode res(-1); ListNode* pre = &res; pre->next = head; bool flag = true; while(head != NULL){ if(head->next == NULL || head->val != head->next->val){ if(!flag) pre->next = head->next; else pre = pre->next; flag = true; }else flag = false; head = head->next; } return res.next; } };
时间: 2024-10-14 00:53:26