题目链接
tag:
- Easy;
- Two Pointers;
question:
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?
思路:
这道题让我们移除链表倒数第N个节点,限定n一定是有效的,即n不会大于链表中的元素总数。还有题目要求我们一次遍历解决问题,那么就得想些比较巧妙的方法了。比如我们首先要考虑的时,如何找到倒数第N个节点,由于只允许一次遍历,所以我们不能用一次完整的遍历来统计链表中元素的个数,而是遍历到对应位置就应该移除了。那么我们需要用两个指针来帮助我们解题,fast
和slow
指针。首先fast
指针先向前走N步,如果此时fast
指向空,说明N为链表的长度,则需要移除的为首元素,那么此时我们返回head->next即可,如果fast
存在,我们再继续往下走,此时slow
指针也跟着走,直到fast
为最后一个元素时停止,此时slow
指向要移除元素的前一个元素,我们再修改指针跳过需要移除的元素即可。代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if (!head || !head->next) return NULL;
ListNode *slow = head, *fast = head;
for (int i=0; i<n; ++i) fast = fast->next;
if (!fast) return head->next;
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return head;
}
};