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

LC_141. Linked List Cycle

时间:2018-02-21 12:49:11      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:shm   etc   style   div   tno   als   head   you   com   

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

标签:shm   etc   style   div   tno   als   head   you   com   

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

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