public class Solution { Set<ListNode> set = new HashSet<>(); public ListNode detectCycle(ListNode head) { if (head == null) { return null; } if (set.contains(head)){ return head; } set.add(head); if (head.next!=null){ return detectCycle(head.next); } return null; } }
|