【leetcode】237. Delete Node in a Linked List
2016-06-03 17:09
423 查看
一、题目描述
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
with value
思路:
一般按课本上面讲的删除链表的一个节点,我们需要知道前一个节点然后将前一个节点的next直接改为要删除节点的next即可,然而本题只知道要删除的节点,那么转换思维。知道了要删除的节点a那么要删除该节点的next节点b不是就可以实现了么,把b的值移到a来,删除b即可。
c++代码(17ms)
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
1 -> 2 -> 3 -> 4and you are given the third node
with value
3, the linked list should become
1 -> 2 -> 4after calling your function.
思路:
一般按课本上面讲的删除链表的一个节点,我们需要知道前一个节点然后将前一个节点的next直接改为要删除节点的next即可,然而本题只知道要删除的节点,那么转换思维。知道了要删除的节点a那么要删除该节点的next节点b不是就可以实现了么,把b的值移到a来,删除b即可。
c++代码(17ms)
#include<iostream> using namespace std; /** * 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){ node->val = node->next->val; node->next = node->next->next; } } };
相关文章推荐
- TMS 例子63 分组,子node
- 18、Dom--childNodes和nodeType的结合使用
- nodejs学习记录(1)
- ionic提示/usr/bin/env: node: 没有那个文件或目录
- Win8.1系统下安装nodeJS
- (java)美团一面之(Populating Next Right Pointers in Each Node)
- 关于在ubuntu系统下nodejs grunt 或者coffee失效的问题
- node-inspector使用
- Zookeeper可视化工具。 ZK 安装 node-zk-browser。2015.10.22亲测可用
- HDFS源码分析心跳汇报之DataNode注册
- nodejs事件模块
- hadoop2.x通过Zookeeper来实现namenode的HA方案以及ResourceManager单点故障的解决方案
- Populating Next Right Pointers in Each NodeII
- Windows下Node.js+Express+WebSocket 安装配置
- gulp入门与一些基本设置
- 237. Delete Node in a Linked List
- 25. Reverse Nodes in k-Group[hard]
- 【入门必备】最佳的 Node.js 学习教程和资料书籍
- 在Windows中安装NodeJS的正确方法
- node 前后端 process.env 共享