删除链表中重复的结点
Posted ssopp24
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除链表中重复的结点相关的知识,希望对你有一定的参考价值。
ListNode* deleteDuplication(ListNode* pHead)
if ( NULL == pHead || NULL == pHead->next )
return pHead;
ListNode* p1 = pHead;
ListNode* p2 = pHead->next;
ListNode* pPrev = pHead;
ListNode* pNewHead = pHead;
if ( p1->val == p2->val )
pPrev = NULL;
pNewHead = NULL;
while ( 1 )
if ( p1->val == p2->val )//在这个大逻辑中,删除节点
while ( p1->val == p2->val )
if ( NULL == p2->next )
if ( NULL == pNewHead )
return NULL;
else
pPrev->next = NULL;
delete p1;
delete p2;
return pNewHead;
else
ListNode* tmp = p2;
p2 = p2->next;
delete tmp;
tmp = NULL;
if ( NULL == pNewHead )
pNewHead = pPrev = p2;
delete p1;
p1 = p2;
if ( NULL == p2->next )
if ( pPrev != p2 )
pPrev->next = p2;
return pNewHead;
else
p2 = p2->next;
if ( p1->val == p2->val )
if ( p1 == pNewHead )
pPrev = NULL;
pNewHead = NULL;
else
if ( p1 != pPrev )
pPrev->next = p1;
pPrev = p1;
p1 = p1->next;
if ( NULL != p2->next )
p2 = p2->next;
else
pPrev->next = p2;
return pNewHead;
在一个有序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
以上是关于删除链表中重复的结点的主要内容,如果未能解决你的问题,请参考以下文章