From 6bfbd3ad207d4c661d6a87909666da2cc52974b8 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sun, 21 Sep 2025 10:23:57 +0800 Subject: [PATCH] Add unit tests for the "Linked List Cycle" problem. --- .../com/leetcode/datastructure/ListNode.java | 18 +++++++ .../leetcode/easy}/LinkedListCycle141.java | 18 +++---- .../leetcode/easy/LinkedListCycle141Test.java | 52 +++++++++++++++++++ 3 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/leetcode/datastructure/ListNode.java rename src/{ => main/java/com/leetcode/easy}/LinkedListCycle141.java (72%) create mode 100644 src/test/java/com/leetcode/easy/LinkedListCycle141Test.java diff --git a/src/main/java/com/leetcode/datastructure/ListNode.java b/src/main/java/com/leetcode/datastructure/ListNode.java new file mode 100644 index 0000000..8839e26 --- /dev/null +++ b/src/main/java/com/leetcode/datastructure/ListNode.java @@ -0,0 +1,18 @@ +package com.leetcode.datastructure; + +public class ListNode { + public int val; + public ListNode next; + + public ListNode() { + } + + public ListNode(int val) { + this.val = val; + } + + public ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} diff --git a/src/LinkedListCycle141.java b/src/main/java/com/leetcode/easy/LinkedListCycle141.java similarity index 72% rename from src/LinkedListCycle141.java rename to src/main/java/com/leetcode/easy/LinkedListCycle141.java index a90f5f7..72c1c25 100644 --- a/src/LinkedListCycle141.java +++ b/src/main/java/com/leetcode/easy/LinkedListCycle141.java @@ -1,8 +1,11 @@ -// Linked List, Hash Table, Two Pointers +// Tags: Linked List, Two Pointers +package com.leetcode.easy; + +import com.leetcode.datastructure.ListNode; public class LinkedListCycle141 { /* - Time complexity : O(n). Let us denote n as the total number of nodes in the linked list. + Time complexity: O(n). Let us denote n as the total number of nodes in the linked list. To analyze its time complexity, we consider the following two cases separately. List has no cycle: @@ -27,7 +30,7 @@ it takes (distance between the 2 runners) / (difference of speed) Therefore, the worst case time complexity is O(N+K), which is O(n). - Space complexity : O(1). We only use two nodes (slow and fast) so the space complexity is O(1). + Space complexity: O(1). We only use two nodes (slow and fast) so the space complexity is O(1). */ public boolean hasCycle(ListNode head) { ListNode slow = head; @@ -41,13 +44,4 @@ public boolean hasCycle(ListNode head) { } return false; } - - public static void main(String[] args) { - ListNode head = new ListNode(3); - ListNode cycleNode = new ListNode(2); - ListNode otherNode = new ListNode(0, new ListNode(-4, cycleNode)); - head.next = cycleNode; - cycleNode.next = otherNode; - System.out.println(new LinkedListCycle141().hasCycle(head)); // true - } } diff --git a/src/test/java/com/leetcode/easy/LinkedListCycle141Test.java b/src/test/java/com/leetcode/easy/LinkedListCycle141Test.java new file mode 100644 index 0000000..468968d --- /dev/null +++ b/src/test/java/com/leetcode/easy/LinkedListCycle141Test.java @@ -0,0 +1,52 @@ +package com.leetcode.easy; + +import com.leetcode.datastructure.ListNode; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class LinkedListCycle141Test { + private LinkedListCycle141 solution; + + @BeforeEach + void setUp() { + solution = new LinkedListCycle141(); + } + + @Test + void testHasCycle_Example1() { + ListNode head = new ListNode(3); + head.next = new ListNode(2); + head.next.next = new ListNode(0); + head.next.next.next = new ListNode(-4); + head.next.next.next.next = head.next; // Create a cycle here + + assertTrue(solution.hasCycle(head)); + } + + @Test + void testHasCycle_Example2() { + ListNode head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = head; // Create a cycle here + + assertTrue(solution.hasCycle(head)); + } + + @Test + void testHasCycle_Example3() { + ListNode head = new ListNode(1); + + assertFalse(solution.hasCycle(head)); + } + + @Test + void testHasCycle_Example4() { + ListNode head = new ListNode(1); + head.next = new ListNode(2); + + assertFalse(solution.hasCycle(head)); + } +}