[LeetCode]Delete Node in a Linked List
2015-07-18 10:00
489 查看
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is
the linked list should become
[思路]只知道当前节点,要删除当前节点。和通常的删除不同的是,我们不知道前一个节点。
我们可以利用当前节点与前一个节点的联系,把后一个节点拷贝过来。然后删除后一个节点,相当于实现了删除操作。
Supposed the linked list is
1 -> 2 -> 3 -> 4and you are given the third node with value
3,
the linked list should become
1 -> 2 -> 4after calling your function.
[思路]只知道当前节点,要删除当前节点。和通常的删除不同的是,我们不知道前一个节点。
我们可以利用当前节点与前一个节点的联系,把后一个节点拷贝过来。然后删除后一个节点,相当于实现了删除操作。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: void deleteNode(ListNode* node) { if(node == nullptr) return; ListNode *next = node->next; node->val = node->next->val; node->next = next->next; delete next; } };
相关文章推荐
- 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
- Populating Next Right Pointers in Each Node
- 【node.js】安装express后,'express' 不是内部或外部命令的问题
- nodejs 调度 node-schedule 使用
- Nodejs正则表达式函数之match、test、exec、search、split、replace使用详解
- Windows环境下的NodeJS+NPM+GIT+Bower安装配置步骤
- win7下,安装node-inspector调试Node.js
- LeetCode Delete Node in a Linked List (删除链表中的元素)
- HDFS DataNode问题-_COPYING_ could only be replicated to 0 nodes instead of minReplication (=1). There
- 简单谈谈PHP vs Node.js
- 《Node入门》 很好的入门读物
- nodejs 应用部署
- nodejs中exports与module.exports的区别