true
otherwise return false
. Consider empty lists as non cyclic.
1->2->3->4->1 ==> true
1->2->3->4 ==> false
'slow'
& 'fast'
where slow
moves one node at a time and fast
moves two nodes at a time. If there is a cycle, these two pointers will inevitably meet! Seriously, they will.
public Boolean isCyclic(ListNode head) { }
C
Java
Python