java:
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode l1 = head, l2 = head;
ListNode l3 = new ListNode(-1);
ListNode res = l3;//注!!需要考虑删除的是head节点
l3.next = head;
for (int i = 0; i < n; i++) {
if (head == null)
return null;
l2 = l2.next;
}
while (l2 != null) {
l1 = l1.next;
l2 = l2.next;
l3 = l3.next;
}
l3.next = l1.next;
return res.next;
}
}