-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
implemented CycleDetectionII code in LinkedList #1482
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e0ddd05
implemented CycleTectionII code
efd1464
changes made per review by appgurueu
ecf1a34
Merge branch 'TheAlgorithms:master' into LLupdates
Akshay73c b543ab0
made the changes per review by appgurueu
0f3cc8f
Merge branch 'master' of https://github.com/TheAlgorithms/JavaScript …
08f3228
changes made per review by appgurueu
3437ca0
Merge branch 'LLupdates' of https://github.com/Akshay73c/JavaScript-a…
90999ea
Merge branch 'master' of https://github.com/Akshay73c/JavaScript-algo…
c957428
did some changes
59d4c09
Merge branch 'TheAlgorithms:master' into LLupdates
Akshay73c 8cabbca
fixed the test file with prettier
1d3d600
Merge branch 'LLupdates' of https://github.com/Akshay73c/JavaScript-a…
a2110f4
Simplify code, renames for clarity
appgurueu 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
There are no files selected for viewing
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
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,57 @@ | ||
/** | ||
* A LinkedList based solution for finding the starting node of the cycle in a list. | ||
* @returns the node where cycle begins in the linked list. If there is no cycle present, returns null. | ||
* @see https://en.wikipedia.org/wiki/Cycle_detection | ||
* @see https://leetcode.com/problems/linked-list-cycle-ii/ | ||
*/ | ||
|
||
function findCycleStart(head) { | ||
let length = 0 | ||
let fast = head | ||
let slow = head | ||
|
||
while (fast !== null && fast.next !== null) { | ||
fast = fast.next.next | ||
slow = slow.next | ||
if (fast === slow) { | ||
length = cycleLength(slow) | ||
break | ||
} | ||
} | ||
|
||
if (length === 0) { | ||
// If there is no cycle, return null. | ||
return null | ||
} | ||
|
||
let ahead = head | ||
let behind = head | ||
// Move slow pointer ahead 'length' of cycle times | ||
while (length > 0) { | ||
ahead = ahead.next | ||
length-- | ||
} | ||
|
||
// Now move both pointers until they meet - this will be the start of cycle | ||
while (ahead !== behind) { | ||
ahead = ahead.next | ||
behind = behind.next | ||
} | ||
|
||
// return the meeting node | ||
return ahead | ||
} | ||
|
||
// head is a node on a cycle | ||
function cycleLength(head) { | ||
// How long until we visit head again? | ||
let cur = head | ||
let len = 0 | ||
do { | ||
cur = cur.next | ||
len++ | ||
} while (cur != head) | ||
return len | ||
} | ||
|
||
export { findCycleStart } |
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,39 @@ | ||
import { findCycleStart } from '../CycleDetectionII' | ||
import { Node } from '../SinglyLinkedList' | ||
|
||
describe('Detect Cycle', () => { | ||
it('no cycle', () => { | ||
const head = new Node(1) | ||
head.next = new Node(2) | ||
|
||
expect(findCycleStart(head)).toBeNull() | ||
}) | ||
|
||
it('simple cycle', () => { | ||
const head = new Node(1) | ||
head.next = new Node(2) | ||
head.next.next = new Node(3) | ||
head.next.next.next = head.next // Creates a cycle | ||
|
||
expect(findCycleStart(head)).toBe(head.next) | ||
}) | ||
|
||
it('long list with cycle', () => { | ||
const head = new Node(1) | ||
head.next = new Node(2) | ||
head.next.next = new Node(3) | ||
head.next.next.next = new Node(4) | ||
head.next.next.next.next = new Node(5) | ||
head.next.next.next.next.next = head.next.next // Cycle | ||
|
||
expect(findCycleStart(head)).toBe(head.next.next) | ||
}) | ||
|
||
it('cycle on last node', () => { | ||
const head = new Node(1) | ||
head.next = new Node(2) | ||
head.next.next = head | ||
|
||
expect(findCycleStart(head)).toBe(head) | ||
}) | ||
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.