【LeetCode】Remove Nth Node From End of List
来源:互联网 发布:触摸屏查询网站源码 编辑:程序博客网 时间:2024/06/02 07:09
题目描述:
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.
第二种是递归,从后往前计算长度,也差不多吧……需要注意下移出第一节点时的处理。
因为if后面的==写成了=,没有one pass,真是想哭……
class Solution {public:ListNode *next;ListNode *removeNthFromEnd(ListNode *head, int n) {next = NULL;if (recusive(head, n) == n)return head->next;return head;}int recusive(ListNode *node, int n){int num = 1;if (node->next)num += recusive(node->next, n);if (num == n - 1)next = node;if (num == n + 1)node->next = next;return num;}};
0 0
- LeetCode[Linked List]: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List leetcode list
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode 40: 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] Remove Nth Node From End of List
- [LeetCode]Remove Nth Node From End of List
- LeetCode-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 - Remove Nth Node From End of List
- LeetCode | Remove Nth Node From End of List
- leetcode之Remove Nth Node From End of List
- JavaScript Closure
- jquery中后代元素和子元素的区别
- [LeetCode55]Jump Game
- Mysql中如何将时间戳转化为常用日期格式
- [C/C++]常见错误积累
- 【LeetCode】Remove Nth Node From End of List
- [LeetCode45]Jump Game II
- html压缩工具html-minifier -- nodejs常用模块(4)
- VS DLL修改lib生成路径
- Swift编程入门
- VS编写Dll
- css压缩工具clean-css -- nodejs常用模块(5)
- 【LeetCode】Valid Parentheses
- 测试WLW