【leetcode c++】19 removeNthFromEnd
来源:互联网 发布:电商大数据分析平台 编辑:程序博客网 时间:2024/06/07 06:44
Given a linked list, remove the nth nodefrom 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 becomes1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
既然给了note,那我们得关注一下,首先,所给的n都是合法的,其次,只允许扫描一遍。
扫描一遍,还要能逆序定位链表的节点,看来我们需要借助关联容器了。
思路:扫描链表的节点,我们以zero-based的下标来作为关联容器的键,链表全部进关联容器之后,我们就可以按下标取节点了,管他顺序逆序。
Leetcode的Accepted Solutions Runtime Distribution(2015-06-05)
源码:(VS2013)如果需要提交leetcode只需要把函数中的代码复制过去即可。
#include <iostream>#include <map>using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};ListNode* removeNthFromEnd(ListNode* head, int n);int main(){ListNode l1(1);ListNode* pl1 = &l1;ListNode l2(2);ListNode* pl2 = &l2;ListNode l3(3);ListNode* pl3 = &l3;ListNode l4(4);ListNode* pl4 = &l4;ListNode l5(5);ListNode* pl5 = &l5;pl1->next = pl2;pl2->next = pl3;pl3->next = pl4;pl4->next = pl5;//cout << removeNthFromEnd(pl1, 2)->val;return 0;}ListNode* removeNthFromEnd(ListNode* head, int n) {//链表如果只有一个节点,那么已知(Given n will always be valid.)条件下//返回的必然是空指针if (!head->next) return NULL;//扫描一次,把链表节点存进map中map<int, ListNode*> myMap;int index = 0;while (head){myMap.insert(pair <int, ListNode*>(index, head));index++;head = head->next;}//注意,我们存进map中的链表zero-based的,此时的index刚好表示链表长度。//因此,(index--)才是表示链表最后一个节点的下标(zero-based)。index--;//那么find(index - (n - 1))就表示倒数第n个节点啦,//倒数第1个的话,代入n=1;,表达式变成find(index)。//倒数第2个的话,代入n=2;,表达式变成find(index-1)。//删除倒数第一个,只要把倒数第二个的next赋空if (1 == n) (--myMap.find((index - (n - 1))))->second->next = NULL;//删除倒数最后一个,即顺数第一个,也简单。else if (n == index + 1){myMap.find(0)->second->next = NULL;//这句注释掉也能过leetcode(2015-06-05),钻牛角尖的感觉return myMap.find(1)->second;}//删除中间的节点,把被删除节点的前驱节点的next赋为被删除节点的后驱节点即可。else (myMap.find((index - (n - 1) - 1)))->second->next = (myMap.find((index - (n - 1) + 1)))->second;return myMap.find(0)->second;}
0 0
- 【leetcode c++】19 removeNthFromEnd
- leetcode 19 removeNthFromEnd
- removeNthFromEnd
- [C++]LeetCode: 19 Partition List
- leetcode: Same Tree (C++)
- [C++]LeetCode: 47 Anagrams
- [C++]LeetCode: 63 Subsets
- LeetCode Majority Element (c++)
- [C++]LeetCode: 81 Triangle
- 【LeetCode】【C++】Wildcard Matching
- 【LeetCode】【C++】Maximal Rectangle
- leetcode(c++) Isomorphic Strings
- 【leetcode c++】twoSum
- C实现 LeetCode-> TwoSum
- 【leetcode c++】ZigZag Conversion
- 【leetcode c++】Reverse Integer
- C实现 LeetCode->LongestCommonPrefix
- C实现 LeetCode->RemoveNthNodeFromEndofList
- 定时cronjob调用Java程序
- H.264 RTP Streaming
- 黑马程序员----2015.6.16笔记(多态,接口,Object,内部类,匿名类,异常,throws)
- 黑马程序员--------常用集合的遍历方式总结
- 简单工厂模式
- 【leetcode c++】19 removeNthFromEnd
- java之从上到下打印二叉树
- KMP算法next数组推导
- αβ-剪枝伪代码
- LeetCode---(202)Happy Number
- You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server versi
- Linux 配置 nfs
- leveldb学习开篇
- 百度更新趋向稳定,我的计划要重新开始