LinkedListCycleII
LinkedListCycleII.java 源码
package datastructure.linkedlist.leetcode;
import java.util.HashSet;
import java.util.Set;
/**
* @author roseduan
* 环形链表II
*/
public class LinkedListCycleII {
public ListNode detectCycle1(ListNode head) {
Set<ListNode> set = new HashSet<>();
while (head != null) {
if (set.contains(head)) {
return head;
}
set.add(head);
head = head.next;
}
return null;
}
public ListNode detectCycle2(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
private static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦