Given a linked list, remove the nth 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.
题目要求do this in one pass ,即要求空间复杂度为O(1)
思路分析
首先,要求删除倒数第 k 个节点,我们肯定要先找到倒数第 k 个节点,通过块满指针来实现,让fast指针先走 k 步,然后 fast 指针和 slow 指针同时走,当 fast 指针走到尽头,则 slow指针所指的节点即为我们要删除的节点,我们需要在遍历的过程中记录 slow 的前一个节点,方便我们找到节点后进行删除