public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) {
return false;
}else if(head.next == head) {
return true;
}
ListNode cur1 = head,cur2 =head;
int count = 0;
boolean find = false;
while(cur2.next != null && cur2.next.next != null) {
cur2 = cur2.next.next;
cur1 = cur1.next;
if(cur1 == cur2) {
find =true;
break;
}
}
return find;
}
141. Linked List Cycle
最新推荐文章于 2024-10-13 15:10:00 发布