末日搭车指南
面向人生编程

导航

 
public boolean hasCycle(ListNode head) {
    Set<ListNode> nodesSeen = new HashSet<>();
    while (head != null) {
        if (nodesSeen.contains(head)) {
            return true;
        } else {
            nodesSeen.add(head);
        }
        head = head.next;
    }
    return false;
}

 

posted on 2020-08-17 03:52  末日搭车指南  阅读(142)  评论(0)    收藏  举报