[leetcode]51 Delete Node in a Linked List
2015-09-06 10:05
513 查看
题目链接:https://leetcode.com/problems/delete-node-in-a-linked-list/
runtime:16ms
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
runtime:16ms
1、问题
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 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
2、分析
这是一道有技巧的题目,咋一看不能解,但是链表的内容是可以变得,将当前节点的内容换成下一个节点的内容,删除下一个节点,解决问题。3、小结
换一个思路,豁然开朗~4、实现
/** * 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) { ListNode *p = node->next; ListNode *q = p->next; node->val = p->val; node->next = q; delete(p); } };
5、反思
略相关文章推荐
- Node.js开发入门—使用对话框ngDialog
- [LeetCode#24]Swap Nodes in Pairs
- 学习nodejs之hello world
- 学习nodejs之restful
- NodeJS入门(五)—— process对象
- leetcode: (41) Populating Next Right Pointers in Each Node
- 基于nodejs实现js后端化处理
- Nodejs-模块-connect源码浅析
- nodejs websocket 事件中传递的参数不对导致nodejs崩溃
- ubuntu14安装node0.12.7
- 深入浅出NodeJS笔记(四)--- 事件订阅与雪崩问题
- Node.js工具集 bower,yeoman,grunt
- node.js 基础学习 express安装使用
- LESS-Middleware:Node.js 和 LESS 的完美搭配
- nodeJS安装(适用于ubuntu和mint系统)
- nodejs小问题:[1]express不是内部或外部命令
- Node.js简介及安装开发环境
- windows系统下简单nodejs安装及环境配置(包括sublime配置)
- Windows7 x64系统下安装Nodejs并在WebStorm 9.0.1下搭建编译less环境
- LeetCode -- Count Complete Tree Node