`
steven-zhou
  • 浏览: 215329 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

判断链表是否有环

阅读更多
/**
 * if has loop return 1 else return 0
 */
static int has_loop(List *list)
{
    List *pFast;
    List *pSlow;

    pFast = pSlow = list;

    if (pFast != NULL && pFast->next != NULL) {
        pFast = pFast->next->next;
    } else {
        return 0;
    }

    while (pFast != pSlow) {
        if (pFast != NULL && pFast->next != NULL)
            pFast = pFast->next->next;
        else
            break;
        pSlow = pSlow->next;
    }

    if (pFast == pSlow)
        return 1;
    else
        return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics