//tc :O(N) N is the length of the linkedList//sc:(1) constant space complexity/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/publicclassSolution{publicbooleanhasCycle(ListNodehead){//we can use double jump to find if loop is present in the loopif(head==null||head.next==null)returnfalse;ListNodenode1=head;ListNodenode2=head.next;while(node1!=null&&node2!=null){if(node1==node2)returntrue;node1=node1.next;node2=node2.next==null?null:node2.next.next;}returnfalse;}}
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)