Created
January 27, 2015 18:41
-
-
Save the-fejw/989573ba696fce35a32a to your computer and use it in GitHub Desktop.
This file contains 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 p1 = head; | |
ListNode p2 = head; | |
while (p2.next != null && p2.next.next != null) { | |
p1 = p1.next; | |
p2 = p2.next.next; | |
if (p1.val == p2.val) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment