237. Delete Node in a Linked List
2015-12-20 02:30
597 查看
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
Subscribe to see which companies asked this question
这题因为指明不会是尾结点,所以我就没对node的后继节点进行是否为空的判断了。这题的主题思想就是并不是直接删除node,而是将node的后继节点的值赋给node节点(值被保存,为后面删除做铺垫),这就说明node的值被覆盖了,这时再删除node的后继节点值,因为会重复,或者说不重要了可删除,就将node的后继指针跨过node->next,而是指向node->next->next,自然node->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
/** * 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的后继节点进行是否为空的判断了。这题的主题思想就是并不是直接删除node,而是将node的后继节点的值赋给node节点(值被保存,为后面删除做铺垫),这就说明node的值被覆盖了,这时再删除node的后继节点值,因为会重复,或者说不重要了可删除,就将node的后继指针跨过node->next,而是指向node->next->next,自然node->next就被从单链表上删掉了
相关文章推荐
- nodejs学习[持续更新]
- 深入浅析Node.js 事件循环
- Delete Node in a Linked List
- Modernizr.js和yepnode.js
- leetcode刷题日记——Delete Node in a Linked List
- nodejs中命令行中参数的获取和解析
- 如何屏蔽nodejs里的/favicon.ico请求
- Leetcode: Count Complete Tree Nodes
- 【leetcode题解】【E】【61.5】Remove Nth Node From End of List
- [Node.js] 使用node-forge保障Javascript应用的传输安全
- node.js环境配置
- Ext系列文件系统inode和block详解
- leetcode -- Count Complete Tree Nodes -- 重点。BFS
- Android系统编译环境初始化时Product产品的import-nodes过程
- 解决NodeJs gm模块报错{ [Error: Command failed: aaЧaaaa ] code: 4, signal: null }
- Nodejs解决2分钟限制
- nodejs基础 -- web模块
- [转]Node.JS package.json 字段全解析
- 带中间件的api调用链|php与nodejs的异同
- nodejs+edatagrid读取本地excel表格