[Leetcode]_19 Remove Nth Node From End of List
来源:互联网 发布:绘图软件下载 编辑:程序博客网 时间:2024/06/10 02:27
/** * Index: 19 * Title: Remove Nth Node From End of List * Author: ltree98 **/
这道题关键在于一遍过。
我想法就是两个指针,相距n个位置,如果前面的指针到底了,那么后面的就是需要被排除的东西。
然后在将一些边界条件判断一下。
/** * 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) { ListNode* slow = head; ListNode* fast = head; while(n > 0) { if(fast) { fast = fast->next; n--; } else return NULL; } if(fast == NULL) { slow = slow->next; return slow; } while(fast->next) { fast = fast->next; slow = slow->next; } ListNode* temp = slow->next; slow->next = temp->next; temp = NULL; return head; }};
0 0
- [Leetcode]_19 Remove Nth Node From End of List
- 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
- VMwareWorkstation10 中安装Centos6.5(64位)图文并茂详细步骤
- Codeforces Round #410 (Div. 2)
- JavaScript模板引擎
- 两个小程序对数组的思考
- 剑指offer-面试题21-包含min函数的栈
- [Leetcode]_19 Remove Nth Node From End of List
- 一步一步学springboot (四)日志管理
- 字符串流的入门——分割排序
- Akka之Hello World
- Golang 学习之路五:表达式
- android 插件化之Activity生命周期之一
- GeoDefense HDU
- 元素为结构体的vector 找最大元素
- 2017年计算机软考(软件设计师)知识点归纳(持续更新中。。。)