问题
0
//tc :O(N) N is the length of the linkedList
//sc:() constant space complexity
/
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) {
//we can use double jump to find if loop is present in the loop
if(head ==null || head.next ==null) return false;
ListNode node = head;
ListNode node= head.next;
while(node!=null && node!=null){
if(node==node) return true;
node = node.next;
node = node.next ==null ? null : node.next.next;
}
return false;
}
}
以上就是检查LinkedList中的循环的详细内容,更多请关注其它相关文章!