Skip to content

Instantly share code, notes, and snippets.

@Cee
Created May 26, 2014 01:32
Show Gist options
  • Save Cee/734e9bf65ac209e9ed90 to your computer and use it in GitHub Desktop.
Save Cee/734e9bf65ac209e9ed90 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 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