Skip to content

Commit 661ee70

Browse files
authored
Merge pull request #3 from sumanas27/ss-linkedList-cycle-detection
LinkedList Cycle Detection
2 parents c30407b + c25c3d6 commit 661ee70

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/main/kotlin/Main.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package main.kotlin
22

3+
import main.kotlin.dsalgoleetcode.LinkedListCycleDetection
4+
import main.kotlin.dsalgoleetcode.ListNode
35
import main.kotlin.dsalgoleetcode.LongestSubstring
46
import main.kotlin.dsalgoleetcode.TwoSum
57

@@ -11,11 +13,21 @@ fun main() {
1113
// Calling Two Sum class
1214
val twoSum = TwoSum()
1315
val result = twoSum.twoSum(nums = intArrayOf(1, 2, 3, 4, 5), target = 9)
16+
println("Indices: ${result.joinToString(",")}")
1417

1518
// Calling Longest Substring
1619
val longestSubstring = LongestSubstring()
1720
val lsResult = longestSubstring.lengthOfLongestSubstring(s = "abcabcbbb")
18-
19-
println("Indices: ${result.joinToString(",")}")
2021
println("Longest Substring : $lsResult")
22+
23+
// Calling LinkedList Cycle Detection
24+
val linkedListCycleDetection = LinkedListCycleDetection()
25+
val node1 = ListNode(1)
26+
val node2 = ListNode(2)
27+
val node3 = ListNode(3)
28+
node1.next = node2
29+
node2.next = node3
30+
node3.next = node1 // cycle back to x
31+
val llcdResult = linkedListCycleDetection.hasCycle(node1)
32+
println("Has cycle $llcdResult")
2133
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
/**
4+
* Linked List Cycle
5+
*
6+
* Given head, the head of a linked list, determine if the linked list has a cycle in it.
7+
* There is a cycle in a linked list if there is some node in the list that can be reached
8+
* again by continuously following the next pointer. Internally, pos is used to denote the
9+
* index of the node that tail's next pointer is connected to. Note that pos is not passed
10+
* as a parameter.
11+
*
12+
* Return true if there is a cycle in the linked list. Otherwise, return false.
13+
*
14+
* Input: head = [3,2,0,-4], pos = 1
15+
* Output: true
16+
* Explanation: There is a cycle in the linked list, where the tail connects to
17+
* the 1st node (0-indexed).
18+
* */
19+
20+
class LinkedListCycleDetection {
21+
22+
/**
23+
* Example:
24+
* var li = ListNode(5)
25+
* var v = li.`val`
26+
* Definition for singly-linked list.
27+
* class ListNode(var `val`: Int) {
28+
* var next: ListNode? = null
29+
* }
30+
*/
31+
32+
fun hasCycle(head: ListNode?): Boolean {
33+
var slow = head
34+
var fast = head
35+
while (fast != null && fast.next != null) {
36+
slow = slow?.next
37+
fast = fast.next?.next
38+
if (slow == fast)
39+
return true
40+
}
41+
return false
42+
}
43+
}
44+
45+
46+
class ListNode(var `val`: Int) {
47+
var next: ListNode? = null
48+
}

0 commit comments

Comments
 (0)