现在的位置: 首页 > 综合 > 正文

Remove duplicates from sorted list

2017年12月23日 ⁄ 综合 ⁄ 共 401字 ⁄ 字号 评论关闭

 

 

 

class Solution {
public:
    ln* deleteDuplicates(ln* head)
{
	if (!head || !head->next)
		return head;
	
	int curVal=head->val;
	int curCnt=1;
	ln* pCur=head->next;
	ln* pTail=head;
	while(pCur)
	{
		if (pCur->val==curVal)
		{
			curCnt++;
			if (curCnt<=2)
			{
				pTail->next=pCur;
				pCur=pCur->next;
			}
			else
			{
				ln* tmp=pCur->next;
				delete pCur;
				pCur=tmp;
			}
		}
		else
		{
			curVal=pCur->val;
			curCnt=1;
			pTail->next=pCur;
                            pTail=pTail->next;
			pCur=pCur->next;
		}
	}
	pTail->next=NULL;
	return head;
}

 

抱歉!评论已关闭.