141. 环形链表

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution 
10 {
11 public:
12     bool hasCycle(ListNode *head) 
13     {
14         ListNode* fast = head;
15         ListNode* slow = head;
16         while(fast && fast->next && slow)
17         {
18             fast = fast->next->next;
19             slow = slow->next;
20             if(fast == slow) return true;
21         }
22         return false;
23     }
24 };

 

posted @ 2020-04-02 18:03  Jinxiaobo0509  阅读(94)  评论(0)    收藏  举报