Movatterモバイル変換


[0]ホーム

URL:


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

Commitcf0593f

Browse files
authored
Refactor Cycledetection.js and added it's test. (TheAlgorithms#1099)
1 parentd115214 commitcf0593f

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
/**
2-
* A LinkedList based solution forDetect a Cycle in a list
2+
* A LinkedList based solution forDetecting a Cycle in a list.
33
* https://en.wikipedia.org/wiki/Cycle_detection
44
*/
55

6-
functionmain(){
6+
functiondetectCycle(head){
77
/*
88
Problem Statement:
99
Given head, the head of a linked list, determine if the linked list has a cycle in it.
10-
11-
Note:
12-
* While Solving the problem in given link below, don't use main() function.
13-
* Just use only the code inside main() function.
14-
* The purpose of using main() function here is to avoid global variables.
15-
1610
Link for the Problem: https://leetcode.com/problems/linked-list-cycle/
1711
*/
18-
consthead=''// Reference to head is given in the problem. So please ignore this line
19-
letfast=head
20-
letslow=head
12+
if(!head){returnfalse}
2113

22-
while(fast!=null&&fast.next!=null&&slow!=null){
14+
letslow=head
15+
letfast=head.next
16+
while(fast&&fast.next){
17+
if(fast===slow){returntrue}
2318
fast=fast.next.next
2419
slow=slow.next
25-
if(fast===slow){
26-
returntrue
27-
}
2820
}
2921
returnfalse
3022
}
3123

32-
main()
24+
export{detectCycle}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import{detectCycle}from'../CycleDetection'
2+
import{Node}from'../SinglyLinkedList'
3+
4+
describe('Detect Cycle',()=>{
5+
it('should detect loop and return true',()=>{
6+
// Creating list and making a loop
7+
constheadNode=newNode(10)
8+
headNode.next=newNode(20)
9+
headNode.next.next=newNode(30)
10+
headNode.next.next.next=newNode(40)
11+
headNode.next.next.next.next=headNode
12+
expect(detectCycle(headNode)).toEqual(true)
13+
})
14+
15+
it('should not detect a loop and return false',()=>{
16+
// Case 0: When head is null, there is no loop.
17+
expect(detectCycle(null)).toEqual(false)
18+
constheadNode=newNode(10)
19+
20+
// Case 1: List with single node doesn't have any loop
21+
expect(detectCycle(headNode)).toEqual(false)
22+
23+
headNode.next=newNode(20)
24+
headNode.next.next=newNode(30)
25+
headNode.next.next.next=newNode(40)
26+
headNode.next.next.next.next=newNode(50)
27+
28+
// Case 2: List not having any loops
29+
expect(detectCycle(headNode)).toEqual(false)
30+
})
31+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp