/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @param {number} n * @return {ListNode} */ const removeNthFromEnd = function(head, n) { // 在头部补一个根节点 解决链表长度为1和2的特殊情况 let node = new ListNode(0, head) let link = node, list = node for(let i = 0; i < n; i++){ link = link.next } while(link && link.next){ link = link.next list = list.next } list.next = list.next ? list.next.next : null return node.next };
标签:node,ListNode,val,list,next,链表,link,节点,倒数第 From: https://www.cnblogs.com/zhenjianyu/p/17081168.html