腾讯云电商网站建设教育培训排行榜前十名
判断链表是否有环
- 1.题目
- 2.思路分析(文字)
- 3.详细的注释和代码实现
1.题目
2.思路分析(文字)
3.详细的注释和代码实现
public class Solution {public boolean hasCycle(ListNode head) {//定义两个快慢指针ListNode fast = head;ListNode slow = head;//让快指针走两步,慢指针走一步while(fast != null && fast.next != null){fast = fast.next.next;slow = slow.next;if(fast == slow){return true;}}return false;}
}