计算机单片机毕设实战-基于 STM32 的 OLED 电梯状态显示控制系统设计 基于单片机的多按键电梯呼叫调度系统实现(016801)
2026/8/1 21:31:50
141. 环形链表
这个题就是滑冰的时候的兔子战术,等快的链表和慢的链表相等的时候说明必有环。
/** * 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) { ListNode slow = head; ListNode fast = head; while(fast!=null&&fast.next!=null) { slow = slow.next; fast = fast.next.next; //如果快的追上了慢的,说明是环形 if(slow == fast) { return true; } } return false; } }