Skip to content

LinkedList Cycle Detection #3

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 1 commit into from
May 18, 2025
Merged
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
16 changes: 14 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main.kotlin

import main.kotlin.dsalgoleetcode.LinkedListCycleDetection
import main.kotlin.dsalgoleetcode.ListNode
import main.kotlin.dsalgoleetcode.LongestSubstring
import main.kotlin.dsalgoleetcode.TwoSum

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

// Calling Longest Substring
val longestSubstring = LongestSubstring()
val lsResult = longestSubstring.lengthOfLongestSubstring(s = "abcabcbbb")

println("Indices: ${result.joinToString(",")}")
println("Longest Substring : $lsResult")

// Calling LinkedList Cycle Detection
val linkedListCycleDetection = LinkedListCycleDetection()
val node1 = ListNode(1)
val node2 = ListNode(2)
val node3 = ListNode(3)
node1.next = node2
node2.next = node3
node3.next = node1 // cycle back to x
val llcdResult = linkedListCycleDetection.hasCycle(node1)
println("Has cycle $llcdResult")
}
48 changes: 48 additions & 0 deletions src/main/kotlin/dsalgoleetcode/LinkedListCycleDetection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main.kotlin.dsalgoleetcode

/**
* Linked List Cycle
*
* Given head, the head of a linked list, determine if the linked list has a cycle in it.
* There is a cycle in a linked list if there is some node in the list that can be reached
* again by continuously following the next pointer. Internally, pos is used to denote the
* index of the node that tail's next pointer is connected to. Note that pos is not passed
* as a parameter.
*
* Return true if there is a cycle in the linked list. Otherwise, return false.
*
* Input: head = [3,2,0,-4], pos = 1
* Output: true
* Explanation: There is a cycle in the linked list, where the tail connects to
* the 1st node (0-indexed).
* */

class LinkedListCycleDetection {

/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/

fun hasCycle(head: ListNode?): Boolean {
var slow = head
var fast = head
while (fast != null && fast.next != null) {
slow = slow?.next
fast = fast.next?.next
if (slow == fast)
return true
}
return false
}
}


class ListNode(var `val`: Int) {
var next: ListNode? = null
}