Remove Nth Node From End of List
2015-07-18 10:52
603 查看
题目:
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.
题意:本题是给定一个链表和一个数字n,找到链表从后往前数第n个节点,删除这个节点,并返回链表头节点head。
思路:
1.因为链表是单向的,不能从后往前数,为了找到从后往前的第n个节点,可以首先计算出链表的节点数num;
2.找到节点总数num,可以定位到第n个节点,但是要考虑几种情况:
(1)链表为空时,直接返回NULL;
(2)当num与n相等时,表示删除头节点;
(3)当num与n不相等时,头节点head不用改变,只需找到倒数第n个节点并删除即可。
代码:
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个节点,删除这个节点,并返回链表头节点head。
思路:
1.因为链表是单向的,不能从后往前数,为了找到从后往前的第n个节点,可以首先计算出链表的节点数num;
2.找到节点总数num,可以定位到第n个节点,但是要考虑几种情况:
(1)链表为空时,直接返回NULL;
(2)当num与n相等时,表示删除头节点;
(3)当num与n不相等时,头节点head不用改变,只需找到倒数第n个节点并删除即可。
代码:
相关文章推荐
- LeetCode 之 Remove Nth Node From End of List — C++ 实现
- LeetCode 133 Remove Nth Node From End of List
- Leetcode: Remove Nth Node From End of List
- leetcode刷题(19. Remove Nth Node From End of List)
- LeetCode 19. Remove Nth Node From End of List--删除链表的倒数第n个结点
- [LeetCode]19. Remove Nth Node From End of List
- leetcode之Remove Nth Node From End of List
- Remove Nth Node From End of List
- LeetCode算法题——19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List
- Remove Nth Node From End of List
- [LeetCode]Delete Node in a Linked List
- leetcode | Swap Nodes in Pairs in a linklist
- ubuntu server 14.10 安装 nodejs
- Delete Node in a Linked List
- Node实践之二
- Windows环境下的nodejs环境开发搭建
- 在hadoop2实际生产环境中,为什么还需要SecondeNamenode
- Delete Node in a Linked List
- datanode无法启动 或 DFS Used% :100 % 或 Live Nodes