首页 > 其他 > 详细

LC_141. Linked List Cycle

时间:2018-02-21 12:39:22      阅读:184      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/linked-list-cycle/description/

 

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

time: o(n) space: o(1)

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

if you use extra space, then it means using hashMap<val, listNode>   time: o(n) space: o(n)

LC_141. Linked List Cycle

原文:https://www.cnblogs.com/davidnyc/p/8456445.html

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