I am looking at the following code from programcreek.com on finding cycles in linked list and wondering how variable 'head' of type ListNode is declared here by passing it as an argument to the boolean method. Is there a data type ListNode in Java. I can't seem to find it in the documentation. Secondly, how do we analyze time and space complexity for this.
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
if(head == null)
return false;
if(head.next == null)
return false;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
return true;
}
return false;
}
}