[Leetcode]Linked List Cycle

来源:互联网 发布:fifa17金币价格淘宝 编辑:程序博客网 时间:2024/06/15 10:06

//判断链表是否有环

class Solution {

public:
    bool hasCycle(ListNode *head) {
        ListNode *first=head;
        ListNode *second=head;
        while(second!=NULL&&second->next!=NULL)
        {
            first=first->next;
            second=second->next->next;
            if(first==second)
            return true;
        }
        return false;
    }
};
0 0
原创粉丝点击