class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (head == NULL)
return NULL;
bool isDup;
ListNode *pos = head, *next_pos = head->next;
if (next_pos == NULL)
return head;
ListNode *pre_pos = new ListNode(0);
ListNode *new_head = pre_pos;
pre_pos->next = head;
while (next_pos != NULL)
{
isDup = false;
while (next_pos != NULL && pos->val == next_pos->val)
{
isDup = true;
next_pos = next_pos->next;
}
if (isDup)
{
pre_pos->next = next_pos;
pos = next_pos;
}
else
{
pre_pos = pre_pos->next;
pos = pos->next;
}
next_pos = next_pos->next;
}
return new_head->next;
}
};
草稿暂存
最新推荐文章于 2024-09-13 18:50:17 发布