Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added CycleNode Algorithm #754

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

Closed
Closed
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
1 change: 1 addition & 0 deletions DIRECTORY.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* [MinPriorityQueue](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Heap/MinPriorityQueue.js)
* Linked-List
* [CycleDetection](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/CycleDetection.js)
* [CycleNode](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/CycleNode.js)
* [DoublyLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/DoublyLinkedList.js)
* [RotateListRight](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/RotateListRight.js)
* [SingleCircularLinkedList](https://github.com/TheAlgorithms/Javascript/blob/master/Data-Structures/Linked-List/SingleCircularLinkedList.js.js)
Expand Down
42 changes: 42 additions & 0 deletions Data-Structures/Linked-List/CycleNode.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Problem: Given a linked list, return the node where cycle begins. If there is no cycle return null

Approach used: Floyd Cycle Detection Algorithm

What is Floyd Cycle Detection Algorithm?
- Floyd Cyclce Detection algorithm uses two pointer (slow & fast), in which fast pointer move twice the speed of slow pointer.
- It is also known as hare and tortoise algorithm as we can imagine slow pointer as tortoise that moves slow and hare as the fast one.
- If the fast pointer reaches null, that means there was no cycle.
- If the fast pointer and slow pointer becomes equal that means there was cycle.

For reference: https://www.codingninjas.com/blog/2020/09/09/floyds-cycle-detection-algorithm/
*/

/*
Input: Pass the head (first node) of linked list
*/
const detectCycleNode = (head) => {
Copy link
Member

@raklaptudirm raklaptudirm Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a specification for head. What type of input do you expect?

Copy link
Member

@raklaptudirm raklaptudirm Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not what I meant. I mean what properties and function do you expect the head parameter to implement. Write a specification for the head parameter.

if (!head || !head.next) {
return null
}

let slow = head
let fast = head

while (fast && fast.next) {
slow = slow.next
fast = fast.next.next
if (slow === fast) break
}

slow = head

while (fast && slow !== fast) {
slow = slow.next
fast = fast.next
}

return fast
}

export { detectCycleNode }

AltStyle によって変換されたページ (->オリジナル) /