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

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

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1, head), slow = dummy, fast = head;
        for(int i = 0; i < n; i++) fast = fast.next;
        while(fast != null){
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }
}

你可能感兴趣的:(#,HOT100,算法)