leetcode_c++:链表:Linked List Cycle(141)

来源:互联网 发布:测音高的软件 编辑:程序博客网 时间:2024/06/05 00:20

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


O(n)


class Solution {public:    bool hasCycle(ListNode *head) {        if(head==NULL ||head->next==NULL) return false;        ListNode* fast=head;        ListNode* slow=head;        do{            slow=slow->next;            fast=fast->next->next;        }while(fast!=NULL && fast->next!=NULL && fast!=slow);            return fast==slow? true:false;    }};
0 0
原创粉丝点击