
// 快慢指针public ListNode removeNthFromEnd(ListNode head, int n) { ListNode fast = head; ListNode slow = head; for (int i = 0;i < n; i++) { fast = fast.next; } if (fast == null) { head = head.next; return head; } while (fast.next != null) { fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return head;}// 递归版本class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { return remove(head,n) == n ? head.next : head; } public int remove (ListNode head, int n) { if (head.next == null) { return 1; } int count = remove(head.next,n); if (count == n) { if (count == 1) { head.next = null; } else { head.next = head.next.next; } } return count + 1; }}