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

leetcode Linked List Cycle

时间:2014-10-04 15:09:06      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:io   for   c   on   amp   r   ad   ef   as   

/**
 * 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 || head->next == NULL){
          return false;
      }  
      
      ListNode *slowP = head;
      ListNode *fastP = head->next;
      
      
      while(slowP != fastP && fastP != NULL && fastP->next != NULL){
          slowP = slowP->next;
          fastP = fastP->next->next;
      }
      
      if(slowP == fastP){
          return true;
      } else {
          return false;
      }
    }
};

leetcode Linked List Cycle

标签:io   for   c   on   amp   r   ad   ef   as   

原文地址:http://blog.csdn.net/wyj7260/article/details/39779263

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