Skip to content

Latest commit

 

History

History
48 lines (39 loc) · 1.18 KB

leetcode_142.md

File metadata and controls

48 lines (39 loc) · 1.18 KB

LeetCode Problems

142. Linked List Cycle II

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

class Solution {
    fun detectCycle(head: ListNode?): ListNode? {
        var cursor: ListNode? = head ?: return null
        var normal: ListNode? = cursor
        var fast: ListNode? = cursor
        // use fast forward node comparison
        while (fast?.next != null && fast?.next?.next != null) {
            normal = normal?.next
            fast = fast?.next?.next
            // if fast forward node catch normal node, it means has cycle.
            if (normal == fast) {
                // move cursor and fast node until meet together by cycle
                while (cursor != fast) {
                    cursor = cursor?.next
                    fast = fast?.next
                }
                return cursor
            }
        }
        return null
    }
}