File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 20
20
* [ 第三章 高质量的代码] ( #第三章-高质量的代码 )
21
21
* [ 16. 数值的整数次方] ( #16-数值的整数次方 )
22
22
* [ 17. 打印从 1 到最大的 n 位数] ( #17-打印从-1-到最大的-n-位数 )
23
+ * [ 18.1 在 O(1) 时间内删除链表节点] ( #181-在-o1-时间内删除链表节点 )
23
24
* [ 18.2 删除链表中重复的结点] ( #182-删除链表中重复的结点 )
24
25
* [ 19. 正则表达式匹配] ( #19-正则表达式匹配 )
25
26
* [ 20. 表示数值的字符串] ( #20-表示数值的字符串 )
@@ -615,6 +616,25 @@ private void printNumber(char[] number) {
615
616
}
616
617
```
617
618
619
+ ## 18.1 在 O(1) 时间内删除链表节点
620
+
621
+ ``` java
622
+ public ListNode deleteNode(ListNode head, ListNode tobeDelete) {
623
+ if (head == null || head. next == null || tobeDelete == null ) return null ;
624
+ if (tobeDelete. next != null ) {
625
+ // 要删除的节点不是尾节点
626
+ ListNode next = tobeDelete. next;
627
+ tobeDelete. val = next. val;
628
+ tobeDelete. next = next. next;
629
+ } else {
630
+ ListNode cur = head;
631
+ while (cur. next != tobeDelete) cur = cur. next;
632
+ cur. next = null ;
633
+ }
634
+ return head;
635
+ }
636
+ ```
637
+
618
638
## 18.2 删除链表中重复的结点
619
639
620
640
``` java
You can’t perform that action at this time.
0 commit comments