使用两个指针去遍历,A指针不停顿的遍历;B指针遇到相同数字时停下,直到与A指针的值不同时或A为NULL时:B的next指向A。
class Solution
{
public:
ListNode *deleteDuplicates(ListNode *head)
{
if (!head || !head->next)
return head;
ListNode *index = head;
ListNode *cur = head;
while (index)
{ //1 1 2 3 3
if (cur == NULL)
{
index->next = NULL;
break;
}
if (index->val != cur->val)
{
index->next = cur;
index = index->next;
}
cur = cur->next;
}
return head;
}
};