// Java
// 从后往前数的递归法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode newhead = new ListNode();
newhead.next = head;
int k = removeOne(newhead, n);
return newhead.next;
}
public int removeOne(ListNode head, int z) {
if (head == null){
return 0;
}
int i = removeOne(head.next, z);
if (i == z){
head.next = head.next.next;
}
i++;
return i;
}
}