feat: 2025-10-23打卡

This commit is contained in:
wu xiangkai
2025-10-23 16:32:13 +08:00
parent 59f245dda7
commit f06095ad7e
9 changed files with 378 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/*
* @lc app=leetcode id=142 lang=java
*
* [142] Linked List Cycle II
*/
// @lc code=start
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* 2 * k - s - (k - s) = p * r
* 2 * k - s - k + s = p * r
* k = p * r, 且 k > s
*
* s + p * r + m - 2m = q * r
* s = q * r - p * r + m
*
* @param head
* @return
*/
public ListNode detectCycle(ListNode head) {
if(head == null) {
return null;
}
Set<ListNode> tracked = new HashSet<>();
ListNode cur = head, r = null;
while(cur != null) {
if(tracked.contains(cur)) {
r = cur;
break;
}
tracked.add(cur);
cur = cur.next;
}
return r;
}
}
// @lc code=end