forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from TheAlgorithms:master #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
Refactor Cycledetection.js and added it's test. (TheAlgorithms#1099)
- Loading branch information
There are no files selected for viewing
24 changes: 8 additions & 16 deletions
Data-Structures/Linked-List/CycleDetection.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,24 @@ | ||
/** | ||
* A LinkedList based solution for Detect a Cycle in a list | ||
* A LinkedList based solution for Detecting a Cycle in a list. | ||
* https://en.wikipedia.org/wiki/Cycle_detection | ||
*/ | ||
|
||
function main () { | ||
function detectCycle (head) { | ||
/* | ||
Problem Statement: | ||
Given head, the head of a linked list, determine if the linked list has a cycle in it. | ||
|
||
Note: | ||
* While Solving the problem in given link below, don't use main() function. | ||
* Just use only the code inside main() function. | ||
* The purpose of using main() function here is to avoid global variables. | ||
|
||
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/ | ||
*/ | ||
const head = '' // Reference to head is given in the problem. So please ignore this line | ||
let fast = head | ||
let slow = head | ||
if (!head) { return false } | ||
|
||
while (fast != null && fast.next != null && slow != null) { | ||
let slow = head | ||
let fast = head.next | ||
while (fast && fast.next) { | ||
if (fast === slow) { return true } | ||
fast = fast.next.next | ||
slow = slow.next | ||
if (fast === slow) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
main() | ||
export { detectCycle } |
31 changes: 31 additions & 0 deletions
Data-Structures/Linked-List/test/CycleDetection.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { detectCycle } from '../CycleDetection' | ||
import { Node } from '../SinglyLinkedList' | ||
|
||
describe('Detect Cycle', () => { | ||
it('should detect loop and return true', () => { | ||
// Creating list and making a loop | ||
const headNode = new Node(10) | ||
headNode.next = new Node(20) | ||
headNode.next.next = new Node(30) | ||
headNode.next.next.next = new Node(40) | ||
headNode.next.next.next.next = headNode | ||
expect(detectCycle(headNode)).toEqual(true) | ||
}) | ||
|
||
it('should not detect a loop and return false', () => { | ||
// Case 0: When head is null, there is no loop. | ||
expect(detectCycle(null)).toEqual(false) | ||
const headNode = new Node(10) | ||
|
||
// Case 1: List with single node doesn't have any loop | ||
expect(detectCycle(headNode)).toEqual(false) | ||
|
||
headNode.next = new Node(20) | ||
headNode.next.next = new Node(30) | ||
headNode.next.next.next = new Node(40) | ||
headNode.next.next.next.next = new Node(50) | ||
|
||
// Case 2: List not having any loops | ||
expect(detectCycle(headNode)).toEqual(false) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.