题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
用栈
import java.util.*;
/*
- public class ListNode {
- int val;
- ListNode next = null;
- }
- /
public class Solution {
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd (ListNode head, int n) {
// write code here
ListNode temp = new ListNode(-4242);
temp.next = head;
int[] nn = {-1};
helper(temp, nn, n);
return temp.next;
}
public static void helper(ListNode temp, int[] nn, int n){ if (temp == null){return;} helper(temp.next, nn, n); nn[0] += 1; if (nn[0] == n){temp.next = temp.next.next;} }
}