leetcode之Linked List Cycle

问题描述如下:

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

问题链接

cpp代码如下:

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head==NULL)return false;
        ListNode* f=head,*s=head;
        do{
            f=f->next;
            if(!f)return false;
            f=f->next;
            if(!f)return false;
            s=s->next;
        }while(f!=s);
        return true;
    }
};


猜你喜欢

转载自blog.csdn.net/wocaoqwerty/article/details/41773875
今日推荐