LeetCode 19. Remove Nth Node From End of List
来源:互联网 发布:行知中学官网 编辑:程序博客网 时间:2024/04/28 08:31
用prev, slow, fast三个指针实现。
prev为slow的前一个指针,fast为slow的后n个指针。
当fast为NULL时,slow即为倒数第n个,此时将其删去即可;我的程序需要特判prev为NULL, 即slow为head的情况。
代码:
class Solution {public: ListNode *removeNthFromEnd(ListNode *head, int n) { ListNode* slow = head, *fast = head, *prev = NULL; for (int i = 0; i<n; ++ i, fast=fast->next) {} while (fast != NULL){ prev = slow;slow = slow->next;fast = fast->next;}// note that we didn't delete the n-th node hereif (prev != NULL){ prev->next = slow->next;} else{ head = head->next;}return head; }};
0 0
- 19. Remove Nth Node From End of List leetcode list
- [LeetCode]19.Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- LeetCode --- 19. 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
- [leetcode] 19.Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- leetcode 19. Remove Nth Node From End of List
- [LeetCode OJ] 19. 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(JAVA)
- leetcode 19. 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
- leetcode 19.Remove Nth Node From End of List
- java 含界面的霍夫曼编码以及解码实现
- 【Android源码编译】在公司内网环境下使用repo获取android源码失败解决办法
- Qt 为程序制作自定义图标
- libsvm工具箱与MATLAB自带工具箱中svmtrian()函数冲突问题解决
- Remove Duplicates from Sorted List II
- LeetCode 19. Remove Nth Node From End of List
- Qt 之 “一般的登录窗口” VS “我写的个性化登录窗口”
- 记录:当常量或变量的所有权转换时的对比
- 在jsp之间传递中文参数出现乱码解决方法
- WordPress主题 大前端 阿里百秀 小清新CMS高级主题[更新v1.1]
- Partition List
- 关于一种投票阵营信息调查问题的研究
- B树、B-树、B+树、B*树
- JQUERY插入,复制、替换和删除节点