题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head)
{
if(head == NULL)
{
return NULL;
}
ListNode* tail = NULL;
ListNode* ret = NULL;
ListNode* next = head->next;
int array[1024] = {-1};
array[0] = head->val;
int top = 0;
ListNode* pre = head;
while(next)
{
if(next->val == array[top])
{
top++;
array[top] = next->val;
}
else if(next->val != array[top])
{
if(top == 0)
{
if(ret == NULL)
{
ret = pre;
tail = pre;
tail->next = NULL;
top = 0;
array[0] = next->val;
}
else
{
tail->next = pre;
tail = pre;
pre->next = NULL;
top = 0;
array[0] = next->val;
}
}
else if(top > 0)
{
top = 0;
array[0] = next->val;
}
}
pre = next;
next = next->next;
}
if(top == 0)
{
if(ret == NULL)
{
//ret = pre;
return pre;
}
tail->next = pre;
tail = pre;
pre->next = NULL;
}
return ret;
}
};