答案,注意,一种是递归,另一种是迭代,那么巧妙利用双指针: 迭代: public static LinkedListNode nthToLast(LinkedListNode head, int n) { LinkedListNode p1 = head; LinkedListNode p2 = head; if (n <= 0) return null; // Move p2 n nodes into the list. Keep n1 in the same position. for (i…
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n w…