3 short Python solutions
Value-Shifting - AC in 64 ms\n\nMy first solution is "cheating" a little. Instead of really removing the nth node, I remove the nth value. I recursively determi
**Value-Shifting - AC in 64 ms**\n\nMy first solution is "cheating" a little. Instead of really removing the nth *node*, I remove the nth *value*. I recursively determine the indexes (counting from back), then shift the values for all indexes larger than n, and then always drop the head.\n\n    class Solution:\n        def removeNthFromEnd(self, head, n):\n            def index(node):\n                if not node:\n                    return 0\n                i = index(node.next) + 1\n                if i > n:\n                    node.next.val = node.val\n                return i\n            index(head)\n            return head.next\n\n---\n\n**Index and Remove - AC in 56 ms**\n\nIn this solution I recursively determine the indexes again, but this time my helper function removes the nth node. It returns two values. The index, as in my first solution, and the possibly changed head of the remaining list.\n\n    class Solution:\n        def removeNthFromEnd(self, head, n):\n            def remove(head):\n                if not head:\n                    return 0, head\n                i, head.next = remove(head.next)\n                return i+1, (head, head.next)[i+1 == n]\n            return remove(head)[1]\n\n---\n\n**n ahead - AC in 48 ms**\n\nThe standard solution, but without a dummy extra node. Instead, I simply handle the special case of removing the head right after the fast cursor got its head start.\n\n    class Solution:\n        def removeNthFromEnd(self, head, n):\n            fast = slow = head\n            for _ in range(n):\n                fast = fast.next\n            if not fast:\n                return head.next\n            while fast.next:\n                fast = fast.next\n                slow = slow.next\n            slow.next = slow.next.next\n            return head
['Python']