题目:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
递归的解法。
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if(pHead==NULL)
return NULL;
if(pHead->next==NULL)
return pHead;
ListNode* cur;
if(pHead->val==pHead->next->val)
{
cur=pHead->next->next;
while(cur&&cur->val==pHead->val)
cur=cur->next;
return deleteDuplication(cur);
}
else
{
pHead->next=deleteDuplication(pHead->next);
return pHead;
}
}
};