ZVVQ代理分享网

检查LinkedList中的循环(linklist遍历)

作者:zvvq博客网
导读问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 //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

问题

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中的循环的详细内容,更多请关注其它相关文章!