编程语言:Java
题目链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
题解:递归即可,注意的是,要是被翻转的位置刚好是开头,则需要手动处理,因为递归中并没有处理。
难度:Medium
结果:AC
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(gogo(head, n)!=n)
return head;
else
return head.next;
}
private static int gogo(ListNode head, int n) {
if (head == null)
return 0;
int ans = gogo(head.next, n);
if (ans == n) {
head.next = head.next.next;
}
return ans + 1;
}
}