Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

141. Linked List Cycle #31

Merged
merged 1 commit into from
Jul 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@ public static int getMiddle(ListNode head) {
}
return slow.val;
}

/**
* Checks if given linked list with {@code head} has cycle
*
* @param head head of a linked list
* @return {@code true} if linked list has cycle, {@code false} otherwise
* @implNote This method runs in {@code O(n)} time complexity and {@code O(1)} space complexity
*/
public static boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ public static ListNode buildLinkedList(int...nums) {
return head;
}

public static ListNode buildLinkedListWithCycle(int[] nums, int cyclePos) {
if (nums == null || nums.length == 0) {
return null;
}
if (cyclePos > nums.length - 1 || cyclePos < -1) {
throw new IllegalArgumentException("cycle position is out of bound");
}

ListNode head = new ListNode(nums[0]);
ListNode current = head;
ListNode cycleNode = (cyclePos == 0) ? head : null;

for (int i = 1; i < nums.length; i++) {
current.next = new ListNode(nums[i]);
current = current.next;
if (i == cyclePos) {
cycleNode = current;
}
}

if (cyclePos != -1) {
current.next = cycleNode;
}

return head;
}


@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,39 @@
import org.junit.jupiter.api.Test;

import static com.xtenzq.linkedlist.FastPointers.getMiddle;
import static com.xtenzq.linkedlist.FastPointers.hasCycle;
import static com.xtenzq.linkedlist.utils.ListNode.buildLinkedList;
import static org.junit.jupiter.api.Assertions.*;
import static com.xtenzq.linkedlist.utils.ListNode.buildLinkedListWithCycle;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class FastPointersTest {

@Test
void middleNode_case1() {
void getMiddle_case1() {
int actual = getMiddle(buildLinkedList(1, 2, 3, 4, 5));
assertEquals(3, actual);
}

@Test
void middleNode_case2() {
void getMiddle_case2() {
int actual = getMiddle(buildLinkedList(1));
assertEquals(1, actual);
}

@Test
void hasCycle_case1() {
assertTrue(hasCycle(buildLinkedListWithCycle(new int[]{3, 2, 0, -4}, 1)));
}

@Test
void hasCycle_case2() {
assertTrue(hasCycle(buildLinkedListWithCycle(new int[]{1, 2}, 0)));
}

@Test
void hasCycle_case3() {
assertFalse(hasCycle(buildLinkedListWithCycle(new int[]{1}, -1)));
}
}
Loading