删掉最后第n个节点。

图解如下:

leetcode 19. Remove Nth Node From End of List

var removeNthFromEnd = function(head, n) {
    var dummy = new ListNode;
    dummy.next = head;
    var fast = dummy, slow = dummy, goSlow = false
    while(fast.next){
        if(goSlow){
            slow = slow.next
        }
        fast = fast.next
        n--
        if(n == 0){
          goSlow = true
        }
    }
    if(slow.next){
        slow.next = slow.next.next
    }
    return dummy.next
    
};

相关文章:

  • 2021-07-16
  • 2021-05-16
  • 2021-07-16
  • 2022-02-16
  • 2021-08-24
  • 2021-09-10
猜你喜欢
  • 2021-10-13
  • 2021-09-02
  • 2021-08-18
  • 2021-10-31
  • 2021-11-23
  • 2021-07-13
相关资源
相似解决方案