Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 744 Bytes

19.md

File metadata and controls

25 lines (21 loc) · 744 Bytes

LeetCode 19 Remove Nth Node From End of List——medium

Given a linked list, remove the n-th node from the end of list and return its head.
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.
Follow up:
Could you do this in one pass?
思路:
头节点有可能被删除、设定一个dummy节点
1、两个指针slow、fast解决one pass问题:
fast先走n步
然后slow、fast一起往前走,一直走到fast.next == null
正好slow会走到需要删除的节点的前一个节点
2、直接删除 slow.next = slow.next.next