Skip to content

Commit 6c16ac8

Browse files
authored
Update 剑指offer面试题18——删除链表的结点.md
1 parent 8a76e51 commit 6c16ac8

File tree

1 file changed

+56
-52
lines changed

1 file changed

+56
-52
lines changed

notes/剑指offer面试题18——删除链表的结点.md

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ package Chap3;
1313
1414
public class DeleteNode {
1515
16+
private Node first;
17+
1618
private class Node {
1719
int val;
1820
Node next;
@@ -21,7 +23,7 @@ public class DeleteNode {
2123
/**
2224
* 常规方法,从first开始找到要删除结点的前一个结点,时间复杂度为O(n)
2325
*/
24-
public void deleteNode_2(Node first, Node toBeDel) {
26+
public void deleteNode_2(Node toBeDel) {
2527
if (first == null || toBeDel == null) {
2628
return;
2729
}
@@ -30,16 +32,17 @@ public class DeleteNode {
3032
first = first.next;
3133
} else {
3234
Node cur = first;
33-
// 找到被删除结点的前一个结点
34-
while (cur.next != toBeDel) {
35+
// 找到被删除结点的前一个结点
36+
while (cur != null && cur.next != toBeDel) {
3537
cur = cur.next;
3638
}
37-
// cur为toBeDel的前一个结点
38-
cur.next = cur.next.next;
39+
if (cur != null) {
40+
// cur为toBeDel的前一个结点
41+
cur.next = cur.next.next;
42+
}
3943
}
4044
}
4145
}
42-
4346
```
4447
4548
试想一个简单例子,下面是一个链表,假设要删除的结点是C。按照上面的思路是从A开始遍历,找到D的前一个结点B后,然后令B.next = D。
@@ -65,29 +68,30 @@ A -> B -> D(new)
6568
更特殊的情况:如果删除的结点既是最后一个结点又是头结点(只有一个结点的链表),那么直接将头结点置空即可。
6669

6770
```java
71+
6872
/**
69-
* 将toBeDel的下一个结点j的值复制给toBeDel。然后将toBeDel指向j的下一个结点
70-
*/
71-
public void deleteNode(Node first, Node toBeDel) {
72-
if (first == null || toBeDel == null) {
73-
return;
74-
}
75-
// 要删除的不是最后一个结点
76-
if (toBeDel.next != null) {
77-
Node p = toBeDel.next;
78-
toBeDel.val = p.val;
79-
toBeDel.next = p.next;
80-
// 是尾结点也是头结点
81-
} else if (first == toBeDel) {
82-
first = first.next;
83-
// 仅仅是尾结点,即在含有多个结点的链表中删除尾结点
84-
} else {
85-
Node cur = first;
86-
while (cur.next != toBeDel) {
87-
cur = cur.next;
88-
}
89-
cur.next = null;
90-
}
73+
* 将toBeDel的下一个结点j的值复制给toBeDel。然后将toBeDel指向j的下一个结点
74+
*/
75+
public void deleteNode(Node toBeDel) {
76+
if (first == null || toBeDel == null) {
77+
return;
78+
}
79+
// 要删除的不是最后一个结点
80+
if (toBeDel.next != null) {
81+
Node p = toBeDel.next;
82+
toBeDel.val = p.val;
83+
toBeDel.next = p.next;
84+
// 是尾结点也是头结点
85+
} else if (first == toBeDel) {
86+
first = first.next;
87+
// 仅仅是尾结点,即在含有多个结点的链表中删除尾结点
88+
} else {
89+
Node cur = first;
90+
while (cur.next != toBeDel) {
91+
cur = cur.next;
92+
}
93+
cur.next = null;
94+
}
9195

9296
}
9397
```
@@ -165,30 +169,30 @@ ListNode cur = pHead;
165169

166170
```java
167171
public ListNode deleteDuplication(ListNode pHead) {
168-
if (pHead == null || pHead.next == null) {
169-
return pHead;
170-
}
171-
// 建立一个头结点代替原来的pHead
172-
ListNode first = new ListNode(pHead.val - 1);
173-
first.next = pHead;
174-
// 当前结点的前一个结点
175-
ListNode pre = first;
176-
// 当前结点
177-
ListNode cur = pHead;
178-
while (cur != null && cur.next != null) {
179-
if (cur.val == cur.next.val) {
180-
int val = cur.val;
181-
while (cur != null && (cur.val == val)) {
182-
cur = cur.next;
183-
}
184-
pre.next = cur;
185-
} else {
186-
pre = cur;
187-
cur = cur.next;
188-
}
189-
}
190-
// 这里不能返回pHead,因为pHead也可能被删除了
191-
return first.next;
172+
if (pHead == null || pHead.next == null) {
173+
return pHead;
174+
}
175+
// 建立一个头结点代替原来的pHead
176+
ListNode first = new ListNode(pHead.val - 1);
177+
first.next = pHead;
178+
// 当前结点的前一个结点
179+
ListNode pre = first;
180+
// 当前结点
181+
ListNode cur = pHead;
182+
while (cur != null && cur.next != null) {
183+
if (cur.val == cur.next.val) {
184+
int val = cur.val;
185+
while (cur != null && (cur.val == val)) {
186+
cur = cur.next;
187+
}
188+
pre.next = cur;
189+
} else {
190+
pre = cur;
191+
cur = cur.next;
192+
}
193+
}
194+
// 这里不能返回pHead,因为pHead也可能被删除了
195+
return first.next;
192196
}
193197
```
194198

0 commit comments

Comments
 (0)