Leetcode:19 Remove Nth Node From End of List(在链表中删除倒数第n个数)
2017-12-13 11:28
721 查看
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.
这道题要求只通过一次遍历就求出结果,问题是这是单向链表,在我们到达最后链表尾端之前,我们不知道我们距离末尾还有多远,这样一来一个最直接的想法就是我们可以先遍历一遍链表得到链表的长度,这样用长度减去指定的n,就知道这个要删除的节点是正着数的第几个,然后再沿着链表去找到这个节点并删除它。但这样的问题就是如果发现要删除的节点是正着数的最后一个,那么我们要删除它, 实际上要在遍历一遍链表,算法总共就遍历了两次链表(求出链表的长度),这就不符合题目要求,虽然还是可以得到答案。
我们现在做的就是只遍历一次,我们的问题是当我们走到链表末端时,我们知道要倒退几个节点,要解决这个问题,我们可以使用两个指针,第一个指针在前面走,第二个指针在它后面距离它n个位置跟着它走,当第一个指针到达最后一个节点时,第二个指针正好在倒数第n+1个节点,这个时候正好进行删除操作,值得注意的是如果第二个指针正好在倒数第n个节点,那么没法进行删除操作。
另外,如果要删除的是第一个节点,那么需要特殊的处理,即直接更新head指针到head->next,即让它指向第二个节点即可。
代码:
4000
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode p=head, q=head; for (int i = n; i > 0 ; i--) { q=q.next; } if(q==null){ return head.next; } // The head need to be removed, do it. q=q.next; while(q!=null){ p=p.next; q=q.next; } p.next=p.next.next; // the one after the p need to be removed return head; }
相关文章推荐
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
- [LeetCode] 19. Remove Nth Node From End of List 删除链表的倒数第N个节点 @python
- LeetCodet题解--19. Remove Nth Node From End of List(删除链表的倒数第n个元素)
- [C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- Leetcode刷题记——19. Remove Nth Node From End of List(删除链表倒数第N项)
- LeetCode 19. Remove Nth Node From End of List--删除链表的倒数第n个结点
- [LeetCode-19] Remove Nth Node From End of List(删除链表中倒数第N个节点)
- leetcode 19. Remove Nth Node From End of List 单向链表删除第n个数
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- LeetCode 19. Remove Nth Node From End of List(删除单链表倒数第N个结点)
- Leetcode Remove Nth Node From End of List 删除链表倒数第n个元素
- leetcode_[python/C++]_19. Remove Nth Node From End of List(删除链表末第n个节点)
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
- Remove Nth Node From End of List 链表删除倒数第N个元素@LeetCode
- leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】
- [LeetCode]—Remove Nth Node From End of List 删除链表的倒数第n个节点
- 【Leetcode】19. Remove Nth Node From End of List - 删掉链表倒数第n个节点