给你一个链表,删除链表的倒数第 n **个结点,并且返回链表的头结点。
输入: head = [1,2,3,4,5], n = 2
输出: [1,2,3,5]
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
var removeNthFromEnd = function(head, n) {
let h=head;
let list=[];
while(h!=null){
list.push(h);
h=h.next;
}
list.push(null);//防止越界
if(n===(list.length-1)){return head.next;}
list[list.length-n-2].next=list[list.length-n];
return head;
};