237. Delete Node in a Linked List
2016-07-02 10:05
351 查看
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
Subscribe to see which companies asked this question
分析:
不要拘泥于遍历然后删除掉某个点。
正确的做法 应该就是拿到这个点后,让这个点的值为下一个点的值,next指向下一个点的next.
删除某节点的两种方法。
一种是跨过这个节点,指向下一个节点。
另一种是赋值这个节点的next节点,然后跨过next节点。对于尾节点,需要另行处理。
本题即属于第二种。
/**
* 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) {
node->val=node->next->val;
node->next=node->next->next;
}
};
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.
Subscribe to see which companies asked this question
分析:
不要拘泥于遍历然后删除掉某个点。
正确的做法 应该就是拿到这个点后,让这个点的值为下一个点的值,next指向下一个点的next.
删除某节点的两种方法。
一种是跨过这个节点,指向下一个节点。
另一种是赋值这个节点的next节点,然后跨过next节点。对于尾节点,需要另行处理。
本题即属于第二种。
/**
* 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) {
node->val=node->next->val;
node->next=node->next->next;
}
};
相关文章推荐
- [Node.js] Creating JWTs (JSON Web Tokens) in Node
- javafx中为一个Node加入多个Effect
- 基于Node的PetShop,oauth2认证RESTful API
- 基于Node的PetShop,oauth2认证RESTful API
- 索引节点和inode的指针结构浅析
- Node.js 4.4.x 更新日志的重要内容回顾
- Ubuntu 16.04 64位 搭建 node.js NodeJS 环境
- HTMLParser使用详解(2)- Node内容
- nssm在windows服务器上部署nodejs
- Leetcode-populating-next-right-pointers-in-each-node-ii ***
- [leetcode]222. Count Complete Tree Nodes
- 全面NodeJs学习教程
- Nodejs开发框架Express3.0
- Linode中的Network Helper
- Nodejs环境配置及命令
- 从Java的角度理解前端框架,nodejs,reactjs,angularjs,requirejs,seajs
- 写一个 nodejs npm应用 - webhere
- 10+ 最佳的 Node.js 教程和实例
- nodejs 下载,安装,测试(windows环境下)
- Leetcode-populating-next-right-pointers-in-each-node