合并单链表,输出单链表中间元素,判断是否有环等 (二)

2014-11-23 22:37:24 · 作者: · 浏览: 9
的指针时,则表示该链表有环

代码如下:


bool is_circle_list(LinkNode *pHead) 
{ 
    LinkNode *pOne = pHead, *pTwo = pHead; 
    while(1) 
    { 
        pOne = pOne->next; 
        pTwo = pTwo->next->next; 
        if(pOne == pTwo) 
            return true; 
        if(pTwo==NULL || pTwo->next==NULL) 
            return false; 
    } 
} 

bool is_circle_list(LinkNode *pHead)
{
 LinkNode *pOne = pHead, *pTwo = pHead;
 while(1)
 {
  pOne = pOne->next;
  pTwo = pTwo->next->next;
  if(pOne == pTwo)
   return true;
  if(pTwo==NULL || pTwo->next==NULL)
   return false;
 }
}