Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/com/leetcode/datastructure/ListNode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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;
Expand All @@ -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
}
}
52 changes: 52 additions & 0 deletions src/test/java/com/leetcode/easy/LinkedListCycle141Test.java
Original file line number Diff line number Diff line change
@@ -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));
}
}