LeetCode_237 Delete Node in a Linked List
2015-07-31 11:31
567 查看
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.
此题只给了要删除的节点,为了保存前后节点的值,可以把下一个节点的值赋给当前节点,把下下给节点赋给当前节点的下一个节点,然后删除下一个节点即可。
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.
此题只给了要删除的节点,为了保存前后节点的值,可以把下一个节点的值赋给当前节点,把下下给节点赋给当前节点的下一个节点,然后删除下一个节点即可。
public void deleteNode(ListNode node) { if(node == null) return; node.val = node.next.val; ListNode tmp = node.next; node.next = tmp.next; }
相关文章推荐
- Cannot delete .... . Name node is in safe mode
- hadoop启动后jps查看总是不显示namenode进程,然后重新格式化hdfs
- node-odata: ASP.NET WEB API OData的替代品
- VFS,super_block,inode,dentry—结构体图解
- [CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径
- java学习脚印:xml中空白文本结点(whitespace TextNode)处理及验证方法
- leetcode之路019 Remove Nth Node From End of List
- NodeJS + PhantomJS 抓取页面信息以及截图
- 利用nodejs+phantomjs+casperjs采集淘宝商品的价格
- Algorithms—237.Delete Node in a Linked List
- datanode无法启动
- 116.Populating Next Right Pointers in Each Node(二叉完全树的next结点)
- nodemailer的使用
- Node.js学习系列总索引
- Nodejs加密php解密
- 使用pm2 管理你的node项目
- hadoop1.2.1的namenode格式化失败的问题
- Bluebird-NodeJs的Promise
- Bluebird-NodeJs的Promise
- NodeJS学习第一季-路由规则