Middle-题目50：142. Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

/**
* Definition for singly-linked list.
* class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
HashSet<ListNode> set = new HashSet<ListNode>();
for(ListNode node=head;node!=null;node=node.next) {
if(!set.add(node))
return node;
}
return null;
}
}

12ms,beats 5.82%，众数1ms,81.12%

【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II（单链表中有环II）】

2015-08-19 06:38:29

(Leetcode 142)Linked List Cycle (II) (快慢指针详解)

2016-03-20 21:05:38

[Leetcode-142] Linked List Cycle II（链表有环详细分析）

2015-10-09 09:33:42

[C语言][LeetCode][142]Linked List Cycle II

2016-04-14 21:50:56

leetcode：142. Linked List Cycle II（Java）解答

2015-12-27 16:36:19

LeetCode(142)Linked List Cycle2

2014-01-10 06:52:27

142. Linked List Cycle II（python）

2016-09-18 21:56:31

LeetCode 142. Linked List Cycle II（C++版）

2017-03-24 20:11:26

142. Linked List Cycle II**

2017-01-08 16:37:06

141. Linked List Cycle [easy] (Python)

2016-05-27 14:37:11

不良信息举报

Middle-题目50：142. Linked List Cycle II