首页 > 其他 > 详细

141. Linked List Cycle

时间:2017-10-24 16:53:15      阅读:235      评论:0      收藏:0      [点我收藏+]

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

题目含义:判断一个列表是否有环路

 

 1     public boolean hasCycle(ListNode head) {
 2         if (head == null) return false;
 3         ListNode walker = head;
 4         ListNode runner = head;
 5         while (runner.next !=null && runner.next.next != null)
 6         {
 7             walker = walker.next;
 8             runner = runner.next.next;
 9             if (walker == runner) return true;
10         }
11         return false;        
12     }

 

141. Linked List Cycle

原文:http://www.cnblogs.com/wzj4858/p/7724234.html

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