[19] 删除链表的倒数第 N 个结点

复制代码
/**
 * 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}
 */
var removeNthFromEnd = function (head, n) {
  const dummyhead = new ListNode(-1);
  dummyhead.next = head;

  let fastindex = dummyhead;
  let slowindex = dummyhead;

  for (let i = 0; i < n + 1; i++) {
    fastindex = fastindex.next;
  }

  while (fastindex !== null) {
    fastindex = fastindex.next;
    slowindex = slowindex.next;
  }

  slowindex.next = slowindex.next.next;

  return dummyhead.next;
};
复制代码

 

posted @   人恒过  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示