首页 > 其他 > 详细

leetcode 之Linked List Cycle(24)

时间:2016-05-21 17:15:57      阅读:148      评论:0      收藏:0      [点我收藏+]

技术分享

两个思路,一是用哈希表记录每个结点是还被访问过;二是定义两个快、慢指针,如果存在环的话,两个指针必定会在某位结点相遇。

技术分享
bool linkListNode(ListNode *head)
      {
          ListNode *fast=head, *slow=head;
          while (fast && fast->next)
          {
              slow = slow->next;
              fast = fast->next->next;

              if (fast == slow)return true;

          }

          return false;
      }
View Code

 

leetcode 之Linked List Cycle(24)

原文:http://www.cnblogs.com/573177885qq/p/5514926.html

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