[0ms][1LINER][100%][Fastest Solution Explained] O(n)time complexity O(n)space complexity
(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care brother, pe
(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *** Kotlin ***\n\n```\n\n/**\n * Example:\n * var li = ListNode(5)\n * var v = li.`val`\n * Definition for singly-linked list.\n * class ListNode(var `val`: Int) {\n *     var next: ListNode? = null\n * }\n */\nclass Solution {\n      fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {\n    if (head == null) return null\n    \n    var nodeCount = 0\n    var current = head\n    \n    while (current != null) {\n      nodeCount++\n      current = current.next\n    }\n    \n    if (nodeCount == n) return head.next\n    \n    var prev: ListNode? = null\n    current = head\n    val dummy = ListNode(-1)\n    dummy.next = current\n    \n    var step = nodeCount - n\n    while (step-- > 0) {\n      prev = current\n      current = current?.next\n    }\n    \n    prev?.next = current?.next\n    \n    return dummy.next\n  }\n}\n\n```\n\n```\n```\n\n```\n```\n\n***"Open your eyes. Expect us." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***
['C', 'PHP', 'C++', 'Java', 'Kotlin', 'JavaScript']