JavaScript One Pass Two Pointer
algorithm\n1. To remove n-th node from the end, send node hare as far as n.\n2. Move node curr and hare in same speed until hare gets the last node.\n3. Since c
**algorithm**\n1. To remove n-th node from the end, send node `hare` as far as `n`.\n2. Move node `curr` and `hare` in same speed until `hare` gets the last node.\n3. Since `curr` and `hare` has gap as `n`, `curr` has n+1-th node from the end when `hare` has 1th node from the end. So change `curr.next` to `curr.next.next`.\n\n**edge case**\n`n = 3` `linked list = [1,2,3]`\n- When `n` is same with the length of the list. We need to remove first element, instead remove next element of `curr`.\n- In this case, you can find that `hare` would be `null`, because the last element of list points `null` such as `[1,2,3,null]`\n\n**complexity**\n- Time complexity: **O(N)**\n- Space complexity: **O(1)**\n```JavaScript\n/**\n * @param {ListNode} head\n * @param {number} n\n * @return {ListNode}\n */\nvar removeNthFromEnd = function(head, n) {\n    let hare = head, curr = head;\n    while (n--) {\n        hare = hare.next;\n    }\n    while (hare && hare.next) {\n        curr = curr.next;\n        hare = hare.next;\n    }\n    if (!hare) {\n        head = head.next;\n    } else {\n        curr.next = curr.next ? curr.next.next : null;\n    }\n    return head;\n};\n```\n\n
['JavaScript']