[Leetcode] [JavaScript] Remove Nth Node from End of List

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.