steven-zhou 发表于 2013-2-4 23:42:17

判断链表是否有环

/** * 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;}
页: [1]
查看完整版本: 判断链表是否有环