首页 > 其他 > 详细

leetcode Linked List Cycle

时间:2014-10-04 14:34:56      阅读:223      评论:0      收藏:0      [点我收藏+]
/**
 * 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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!