LeetCode OJ:Delete Node in a Linked List(链表节点删除)
2015-10-18 10:19
751 查看
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
更改当前节点的值来实现
没什么好说的,下面是java版本的:
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 == NULL) return; node->val = node->next->val; node->next = node->next->next; } };
没什么好说的,下面是java版本的:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } }
相关文章推荐
- node-mysql 在4.2.0的时候遇到的错误
- Nodejs-搭建Nodejs开发环境
- LeetCode解题报告--Reverse Nodes in k-Group
- nodejs生成多层目录和生成文件的通用方法
- Nodejs中间件Connect
- VB读取XML文件,selectNodes获取节点属性
- Node.js代码总结
- Reverse Nodes in k-Group
- nodejs中express入口文件及supervisor插件问题
- Swap Nodes in Pairs
- Remove Nth Node From End of List
- nodejs支持ssi实现include shtml页面
- <LeetCode><Easy> 19 Remove Nth Node From End of List
- LeetCode解题报告--Swap Nodes in Pairs
- [leetcode] Swap Nodes in Pairs
- assert()断言函数以及NODEBUG用法总结
- hadoop nameNode 无法启动
- 使用 AngularJS & NodeJS 实现基于 token 的认证
- nodejs 计算内存使用率
- Reverse Nodes in k-Group