13 在 O(1) 时间内删除链表节点
2024-08-24 11:04:48
删除链表的一个结点,用下一个结点覆盖掉要删除的结点,再释放掉要删结点的下一个结点的内存
Java:
public ListNode deleteNode(ListNode head, ListNode tobeDelete) {
if (head == null || head.next == null || tobeDelete == null) return null;
if (tobeDelete.next != null) {
// 要删除的节点不是尾节点
ListNode next = tobeDelete.next;
tobeDelete.val = next.val;
tobeDelete.next = next.next;
} else {
ListNode cur = head;
while (cur.next != tobeDelete) cur = cur.next;
cur.next = null;
}
return head;
}
最新文章
- Lesson 7 Too late
- UIToolBar
- UI第十三节——UIActionSheet
- pip和easy_install更换使用国内源
- Ubuntu下使用SVN
- [Tools] Eclipse使用小技巧-持续更新
- java 程序访问hdfs错误 hadoop2.2.0
- (zz) 谷歌技术";三宝";之BigTable
- JMeter教程01-下载和安装
- http://jingyan.baidu.com/article/fcb5aff78e6a48edab4a7146.html
- List null
- Java 中的抽象类及接口
- Android之开源项目view篇
- DependencyProperty
- 解析word中的表格
- CSS的标签类型
- bzoj4826 [Hnoi2017]影魔
- DIY 温控烙铁
- 下一个ajax异步请求被挂起问题
- .net core EFCore CodeFirst 迁移出现错误【No project was found. Change the current working directory or use the --project option. 】