leetcode之Linked List Cycle

来源:互联网 发布:英国留学移民知乎 编辑:程序博客网 时间:2024/05/17 01:53

问题描述如下:

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;    }};


0 0
原创粉丝点击