Created
May 26, 2014 01:32
-
-
Save Cee/734e9bf65ac209e9ed90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } | |
*/ | |
public class Solution { | |
public boolean hasCycle(ListNode head) { | |
if (head == null) return false; | |
ListNode first = head; | |
ListNode second = head; | |
while ((second.next != null) && (second.next.next != null)){ | |
first = first.next; | |
second = second.next.next; | |
if (first == second) return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment