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 newHead = new ListNode(-1); ListNode tail = head; newHead.next = head; ListNode pre = newHead; while (n != 1) { tail = tail.next; n--; } ListNode cur = head; while (tail.next != null) { cur = cur.next; pre = pre.next; tail = tail.next; } pre.next = cur.next; cur.next = null; return newHead.next; } }