k*******t 发帖数: 144 | 2 两个指针(slow, fast)就够啦,判断list有无loop也不需要额外空间。贴了个代码,比
较ugly, 凑合着看吧
bool hasLoop(int arr[], int len){
int slow = 0;
int fast = 0;
int cur = 0;
while(cur < len)
{
if(arr[slow] != -1)
{
slow = arr[slow];
}
else
{
++slow;
fast = slow;
cur = slow;
continue;
}
int i = 0;
while(i < 2 && arr[fast] != -1)
{
fast = arr[fast];
... 阅读全帖 |
|