Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ListNode fast = head, slow = head; 6 while(fast != null && fast.next != null){ 7 fast = fast.next.next; 8 slow = slow.next; 9 if(fast == slow) return true; 10 } 11 return false; 12 } 13 }
posted on 2013-11-26 09:30 Step-BY-Step 阅读(133) 评论(0) 编辑 收藏 举报