/** * 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) { if (head == null){ return false; } ListNode slow = head; ListNode fast = head; while(fast.next != null && fast.next.next != null){ slow = slow.next; fast = fast.next.next; if (fast.val == slow.val){ return true; } } return false; } }
Linked List Cycle
最新推荐文章于 2022-12-06 22:57:02 发布