Skip to content

Instantly share code, notes, and snippets.

@the-fejw
Created January 27, 2015 18:41
Show Gist options
  • Save the-fejw/989573ba696fce35a32a to your computer and use it in GitHub Desktop.
Save the-fejw/989573ba696fce35a32a to your computer and use it in GitHub Desktop.
/**
* 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