牛客题霸NC4 判断链表中是否有环Java题解
题目描述
判断给定的链表中是否有环。如果有环则返回true,否则返回false。
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null){
return false;
}
ListNode l1 = head;
ListNode l2 = head;
while(l2!=null && l2.next!=null){
l2 = l2.next.next;
l1 = l1.next;
if(l1 == l2){
return true;
}
}
return false;
}
}
#题解##牛客题霸#
查看13道真题和解析

