Linked List Cycle

来源:互联网 发布:西部网络达人秀开场舞 编辑:程序博客网 时间:2024/06/05 15:46
/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    bool hasCycle(ListNode *head)     {                if(head==NULL)            return false;        ListNode *slow=head;        ListNode *fast=head->next;        while(fast&&fast->next)        {            slow=slow->next;            fast=fast->next->next;            if(slow==fast)                return true;        }                return false;    }};

0 0