[C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
2015-05-02 20:23
666 查看
Problem:
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Note:
Given n will always be valid.
Try to do this in one pass.
分析:
也是出现过很多次的基本算法题。
考察点是两指针的联合使用,先让指针1走n步,再一起走,直到指针1的next为NULL,此时指针2的next为要删除的节点。两指针联合使用的经典问题还有判断链表是否有环。
题目中说了Given n will always be valid.,但码代码的时候为保持完整性,还是考虑了invalid情况。
AC Code(C++):
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.
分析:
也是出现过很多次的基本算法题。
考察点是两指针的联合使用,先让指针1走n步,再一起走,直到指针1的next为NULL,此时指针2的next为要删除的节点。两指针联合使用的经典问题还有判断链表是否有环。
题目中说了Given n will always be valid.,但码代码的时候为保持完整性,还是考虑了invalid情况。
AC Code(C++):
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: //207 / 207 test cases passed. //Runtime: 8 ms //虽然题设说了Given n will always be valid,但为保持完整性还是考虑了invalid的情况 ListNode* removeNthFromEnd(ListNode* head, int n) { if (head == NULL || n < 1){//无效输入 return head; } ListNode *pNode1 = head; //pNode1先走n步 for (int step = 0; step < n; ++step) { if (pNode1 == NULL) {//n无效 return head; } pNode1 = pNode1->next; } if (pNode1 == NULL) {//要删除的是第一个节点 ListNode *pTemp = head->next; delete head; return pTemp; } //两指针一起走,当pNode1走到pNode1->next == NULL时,pNode2走到倒数第n+1 ListNode *pNode2 = head; while (pNode1->next != NULL) { pNode1 = pNode1->next; pNode2 = pNode2->next; } //pNode2的next为要删除节点 ListNode *pTemp = pNode2->next; pNode2->next = pNode2->next->next; delete pTemp; return head; } };
相关文章推荐
- [LeetCode] 19. Remove Nth Node From End of List 删除链表的倒数第N个节点 @python
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
- [LeetCode-19] 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个节点- Easy +
- 【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个节点
- 174. 删除链表中倒数第n个节点 (remove-nth-node-from-end-of-list)(c++)----lintcode面试题之链表
- LeetCode 19. Remove Nth Node From End of List--删除链表的倒数第n个结点
- LeetCodet题解--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(Remove Nth Node From End of List)删除链表倒数第n个节点
- [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点
- Remove Nth Node From End of List 删除链表倒数第N个节点
- 19. Remove Nth Node From End of List (移除链表倒数第n个节点)
- Leetcode刷题记——19. Remove Nth Node From End of List(删除链表倒数第N项)
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点