Python/Go/JS/C++ O(n) by two-pointers [w/ Visualization]
O(n) one-pass by two-pointers and delay\n\n---\n\nHint:\n\nThink of two-pointers with n-step delay.\n\nFirst pointer keeps going till the end.\nSecond pointer t
O(n) one-pass by two-pointers and delay\n\n---\n\n**Hint**:\n\nThink of **two-pointers** with **n-step delay**.\n\nFirst pointer keeps going till the end.\nSecond pointer traverses to the previous node of the one being removed with n-step delay.\n\nWhen first pointer reach the end, the second one will be on the right position.\nThen update linkage of second pointer and remove the N-th node from the end.\n\n---\n\n**Visualization & Diagram**:\n\n![image](https://assets.leetcode.com/users/brianchiang_tw/image_1588588180.png)\n\n---\n\n![image](https://assets.leetcode.com/users/brianchiang_tw/image_1588588255.png)\n\n---\n\n![image](https://assets.leetcode.com/users/brianchiang_tw/image_1588588288.png)\n\n---\n\n![image](https://assets.leetcode.com/users/brianchiang_tw/image_1588588310.png)\n\n---\n\n**Implementation** by two-pointers and delay in Python:\n\n```\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\n\nclass Solution:\n    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:\n        \n        # use dummy head will make the removal of head node easier\n        dummy_head = ListNode(-1)\n        dummy_head.next = head\n        \n        # cur keeps iteration till the end\n        # prev_of_removal traverses to the previous node of the one of being removed\n        cur, prev_of_removal = dummy_head, dummy_head\n        \n        \n        while cur.next != None:\n            \n            # n-step delay for prev_of_removal\n            if n <= 0:\n                prev_of_removal = prev_of_removal.next\n                \n            cur = cur.next\n            \n            n -=1\n        \n        \n        # Remove the N-th node from end of list\n        n_th_node = prev_of_removal.next\n        prev_of_removal.next = n_th_node.next\n        \n        del n_th_node\n        \n        return dummy_head.next\n```\n\n---\n\nJavascript:\n\n```\n/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @param {number} n\n * @return {ListNode}\n */\nvar removeNthFromEnd = function(head, n) {\n    \n    // use dummy head will make the removal of head node easier\n    let dummyHead = new ListNode( -1, head);\n    \n    // cur keeps iteration till the end\n    // prev_of_removal traverses to the previous node of the one of being removed    \n    let cur = dummyHead;\n    let prevOfRemoval = dummyHead;\n    \n    while( cur.next != null ){\n        \n        // n-step delay for prevOfRemoval\n        if( n <= 0 ){\n            prevOfRemoval = prevOfRemoval.next;\n        }\n        \n        cur = cur.next;\n        \n        // update counter of n-step delay\n        n -= 1;\n    }\n    \n    \n    nThNode = prevOfRemoval.next;\n    prevOfRemoval.next = nThNode.next;\n    \n    return dummyHead.next;\n    \n};\n```\n\n---\n\n**Implementation** by two-pointers and delay in Go:\n\n```\n/**\n * Definition for singly-linked list.\n * type ListNode struct {\n *     Val int\n *     Next *ListNode\n * }\n */\n\nfunc removeNthFromEnd(head *ListNode, n int) *ListNode {\n    \n    dummyHead := &ListNode{-1, head}\n    \n    cur, prevOfRemoval := dummyHead, dummyHead\n    \n    for cur.Next != nil{\n        \n        // n step delay for prevOfRemoval\n        if n <= 0 {\n            prevOfRemoval = prevOfRemoval.Next\n        }\n        \n        cur = cur.Next\n        \n        n -= 1\n    }\n    \n    // Remove the N-th node from end of list\n    nthNode := prevOfRemoval.Next\n    prevOfRemoval.Next = nthNode.Next\n    \n    return dummyHead.Next\n    \n}\n```\n\n---\n\nC++\n\n```\nclass Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        \n        // use dummy head will make the removal of head node easier\n        ListNode* dummyHead = new ListNode(-1, head);\n        \n        // cur keeps iteration till the end\n        // prevOfRemoval traverses to the previous node of the one of being removied\n        ListNode* cur = dummyHead;\n        ListNode* prevOfRemoval = dummyHead;\n        \n        while( cur->next != nullptr ){\n            \n            // n-step delay for prevOfRemoval\n            if( n <= 0 ){\n                prevOfRemoval = prevOfRemoval->next;\n            }\n            \n            cur = cur->next;\n            \n            // update counter of n step delay\n            n -= 1;\n        }\n        \n        // Remove the n-th node from end of list\n        ListNode* nThNode = prevOfRemoval->next;\n        prevOfRemoval->next = nThNode->next;\n        \n        delete nThNode;\n        \n        return dummyHead->next;\n        \n        \n        \n    }\n};\n```
['Two Pointers', 'C', 'Iterator', 'Python', 'Go', 'Python3', 'JavaScript']