141. 环形链表 141. 环形链表方法一:哈希表方法二:快慢指针 141. 环形链表 方法一:哈希表 public class Solution { public boolean hasCycle(ListNode head) { HashSet<ListNode> seen = new HashSet<>(); while (head != null) { if (!seen.add(head)) { return true; } head = head.next; } return false; } } 方法二:快慢指针 public class Solution { public boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return false; } ListNode slow = head; ListNode fast = head.next; while (slow != fast) { if (fast == null || fast.next == null) { return false; } slow = slow.next; fast = fast.next.next; } return true; } }