题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
//哈哈,水过了,终于做完十个题了(苦笑)
class Solution {
public:
/**
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* deleteDuplicates(ListNode* head) {
// write code here
if(head==NULL||head->next==NULL)
return head;
ListNode *really,*pre,*qre;
ListNode* first = new ListNode(0);
first->next=head;
really=first;
pre=head;
qre=head->next;
while(qre)
{
if(pre->val!=qre->val)
{
really->next=pre;
really=really->next;
pre=qre;
qre=qre->next;
}
else{
while(pre->val==qre->val&&qre)
{
qre=qre->next;
}
if(qre==NULL)
{
really->next=NULL;
return first->next;
}
pre=qre;
qre=qre->next;
}
}
really->next=pre;
really->next->next=NULL;
return first->next;
}
};
查看9道真题和解析