码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode141 Linked List Cycle

时间:2018-10-20 00:46:47      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:ret   efi   turn   strong   思路   targe   title   pre   solution   

思路:

floyed判环法。

https://www.youtube.com/watch?v=LUm2ABqAs1w

实现:

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

 

leetcode141 Linked List Cycle

标签:ret   efi   turn   strong   思路   targe   title   pre   solution   

原文地址:https://www.cnblogs.com/wangyiming/p/9819962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!