1.题目描述
Given a linked list, remove the n-th node from the end of list and return its head.
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
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?
你能尝试使用一趟扫描实现吗?
2.Solutions
快慢指针(双指针),把慢指针定位到要删除节点的前面就可以了。
1 | public static void main(String[] args) { |