https://leetcode.com/problems/remove-nth-node-from-end-of-list/
Remove Nth Node from End of List
Given A linked list, remove the nth node from the end of the list and return its head.
For example,
n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n would always be valid.
Try to do the in one pass.
The tip says that the loop can be done. Open the array to the elements of the list (JS is the object of the reference), the subscript is the order of the arrays, as long as the number of the countdown to the nth group of elements, he pointed to his former one is good. The exception boundary value is nothing more than the previous one, and there is only one element in the list. I have to spit groove js good pit ah, empty words should not return {}, node nodes are objects Ah, he actually need to return [].
1 /**2 * @param {listnode} head3 * @param {number} n4 * @return {ListNode}5 */6 varRemoventhfromend =function(head, n) {7 varCache = [];8 vari = 0;9 varCurrent =head;Ten while(current) { OneCache[i] =Current ; Ai++; -Current =Current.next; - } the - varindex = cache.length-N; - - if(!cache[index-1]){ + returnCACHE[1] | | []; -}Else if(!cache[index + 1]){ +Cache[index-1].next =NULL; A returnCache[0]; at}Else{ -Cache[index-1].next = Cache[index + 1]; - returnCache[0]; - } -};
[Leetcode] [JavaScript] Remove Nth Node from End of List