删除链表的倒数第n个节点
删除链表的倒数第n个节点
http://www.nowcoder.com/questionTerminal/f95dcdafbde44b22a6d741baf71653f6
使用快慢指针
快指针先走n步,然后再和慢指针一起走,此时当快指针走完的时候,慢指针则刚好走到倒数的第n个结点。
public ListNode removeNthFromEnd (ListNode head, int n) { // write code here ListNode pre = new ListNode(0); pre.next = head; ListNode start = pre; ListNode end = pre; while(n != 0){ start =start.next; n--; } while(start.next != null){ start = start.next; end = end.next; } end.next = end.next.next; return pre.next; }