题目 
给定一个链表,删除链表的倒数第 n个结点,并且返回链表的头结点。
示例 1:

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]1
2
2
示例 2:
输入:head = [1], n = 1
输出:[]1
2
2
示例 3:
输入:head = [1,2], n = 1
输出:[1]1
2
2
提示:
- 链表中结点的数目为 sz
- 1 <= sz <= 30
- 0 <= Node.val <= 100
- 1 <= n <= sz
**进阶:**能尝试使用一趟扫描实现吗?
注意:本题与主站 19 题相同: https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
题解 
java
public ListNode removeNthFromEnd(ListNode head, int n) {
    // 缓存位置及节点
    ListNode[] nodes = new ListNode[30];
    ListNode cursor = head;
    // 节点数量
    int cnt = 0;
    while (cursor != null) {
        nodes[cnt++] = cursor;
        cursor = cursor.next;
    }
    // 只有一个节点
    if (cnt == 1) {
        return null;
    }
    if (n == cnt) {
        // 头结点
        head = head.next;
    } else if (n == 1) {
        // 最后一个节点 将尾节点置空
        nodes[cnt - 2].next = null;
    } else {
        // 中间节点
        nodes[cnt - n - 1].next = nodes[cnt - n + 1];
    }
    return head;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30