1 class Solution {
2 public ListNode removeNthFromEnd(ListNode head, int n) {
3 if(head == null) return head;
4 if(head.next == null) return null;
5 ListNode node1 = head;
6 ListNode node2 = head;
7 ListNode p = node1;
8 for(int i = 0; i < n - 1; i++) {
9 node2 = node2.next;
10 }
11 if(node2.next == null) { //要加上这一段 要是node2刚好到tail 下面代码无法更新node1, 因为node1在head
12 return head.next;
13 }
14 while(node2.next != null) {
15 p = node1;
16 node1 = node1.next;
17 node2 = node2.next;
18 }
19 p.next = node1.next;
20 return head;
21 }
22 }