public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode first = dummy;
        ListNode second = dummy;
        // Advances first pointer so that the gap between first and second is n nodes apart
        for (int i = 1; i <= n + 1; i++) {
            first = first.next;
        }
        // Move first to the end, maintaining the gap
        while (first != null) {
            first = first.next;
            second = second.next;
        }
        second.next = second.next.next;
        return dummy.next;
    }

 

相关文章:

  • 2022-12-23
  • 2018-07-07
  • 2021-04-25
  • 2021-11-27
  • 2021-07-28
  • 2022-12-23
猜你喜欢
  • 2020-10-18
  • 2021-09-24
  • 2021-11-29
  • 2021-04-01
  • 2021-11-05
  • 2022-12-23
相关资源
相似解决方案