模拟即可:
Accode:
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode list = new ListNode(0, head);
int length = getLength(head);
ListNode cur = list;
for (int i = 1; i <= length - n ; ++i) {
cur = cur.next;
}
cur.next = cur.next.next;
return list.next;
}
public int getLength(ListNode head) {
int length = 0;
while (head != null) {
++length;
head = head.next;
}
return length;
}
}
over~