Forráskód Böngészése

#feat:leetcode p142题题解

yangyi 2 hete
szülő
commit
a90e10ac47
2 módosított fájl, 129 hozzáadás és 0 törlés
  1. 68 0
      src/leetcode/p142/Solution.java
  2. 61 0
      src/leetcode/p142/SolutionTest.java

+ 68 - 0
src/leetcode/p142/Solution.java

@@ -0,0 +1,68 @@
+package leetcode.p142;
+
+import java.util.HashSet;
+import java.util.Objects;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/7 14:40
+ * @Description: https://leetcode.cn/problems/linked-list-cycle-ii/description/
+ * 142. 环形链表 II
+ */
+public class Solution {
+    public ListNode detectCycle(ListNode head) {
+        //快慢指针
+        //Floyd 判圈算法
+        ListNode fast = head;
+        ListNode slow = head;
+        while (fast != null && fast.next != null){
+            fast = fast.next.next;
+            slow = slow.next;
+            //有环
+            if (slow.equals(fast)) {
+                //从相遇点和头节点同时以相同的速度移动,下一次相遇点就是环的入口
+                while (!head.equals(slow)){
+                    slow = slow.next;
+                    head = head.next;
+                }
+                return head;
+            }
+        }
+        return null;
+    }
+    private ListNode hashSolution(ListNode head){
+        //哈希表
+        HashSet<ListNode> set = new HashSet<>();
+        while(head !=null){
+            if (!set.contains(head)){
+                set.add(head);
+            }else {
+                return head;
+            }
+            head = head.next;
+        }
+        return null;
+    }
+}
+class ListNode {
+    int val;
+    ListNode next;
+    ListNode() {}
+    ListNode(int val) { this.val = val; }
+    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        ListNode listNode = (ListNode) o;
+        return val == listNode.val;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(val, next);
+    }
+}

+ 61 - 0
src/leetcode/p142/SolutionTest.java

@@ -0,0 +1,61 @@
+package leetcode.p142;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2026/3/7 14:50
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    public static final Solution solution = new Solution();
+    private ListNode head;
+    private ListNode expected;
+
+    public SolutionTest(ListNode head, ListNode expected) {
+        this.head = head;
+        this.expected = expected;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        ArrayList<Object[]> list = new ArrayList<>();
+
+        ListNode head = new ListNode(3);
+        ListNode node2 = new ListNode(2);
+        ListNode node3 = new ListNode(0);
+        ListNode node4 = new ListNode(-4);
+
+        head.next = node2;
+        node2.next = node3;
+        node3.next = node4;
+        node4.next = node2;
+        list.add(new Object[]{head, node2});
+
+        head = new ListNode(1);
+        node2 = new ListNode(2);
+        head.next = node2;
+        node2.next = head;
+        list.add(new Object[]{head, head});
+
+        head = new ListNode(1);
+        list.add(new Object[]{head, null});
+
+        return list;
+    }
+
+    @Test
+    public void detectCycle() {
+        ListNode listNode = solution.detectCycle(head);
+        assertEquals(expected, listNode);
+    }
+}