Skip to content

Latest commit

 

History

History
25 lines (12 loc) · 625 Bytes

remove-nth-node-from-end-of-list.md

File metadata and controls

25 lines (12 loc) · 625 Bytes

Remove Nth Node From End of List

描述

Given a linked list, remove the n-th node from the end of list and return its head.

For 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 will always be valid.
  • Try to do this in one pass.

分析

设两个指针p,q,让q先走n步,然后pq一起走,直到q走到尾节点,删除p->next即可。

代码

{% codesnippet "./code/remove-nth-node-from-end-of-list."+book.suffix, language=book.suffix %}{% endcodesnippet %}