1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0,head); removeFromEnd(dummy, n); return dummy.next; } public int removeFromEnd(ListNode head, int n) { if (head == null) return 0; int num = removeFromEnd(head.next, n) + 1; if (num == n + 1) { head.next = head.next.next; } return num; }
|