题目

解题思路

  1. 记录曾经走过的节点,如果再次出现则存在环,否则该链表不存在环。

代码展示

public class Solution {
    public ListNode detectCycle(ListNode head) {
        List<ListNode> store = new ArrayList<>();
        while (head != null){
            if(store.contains(head)){
                return head;
            } else {
                store.add(head);
            }
            head = head.next;
        }
        return null;
    }
}
11-28 18:22