今天刷的另一道题是LeetCode第141题,环形链表,这儿题也不是很难,直接快慢指针就解决了,具体地代码如下:
public boolean hasCycle(ListNode head) {
ListNode fast=head;
if (head==null){
return false;
}
if (head.next==head){
return true;
}
while (fast.next!=null){
if (fast.next.next==null){
return false;
}
fast=fast.next.next;
head=head.next;
if (fast==head){
return true;
}
}
return false;
}