Skip to content

Commit fc9ed9b

Browse files
committed
feat: update solutions to lcof problem: No.18
面试题18.删除链表的节点
1 parent 46ffe9d commit fc9ed9b

File tree

6 files changed

+76
-120
lines changed

6 files changed

+76
-120
lines changed

lcof/面试题18. 删除链表的节点/README.md

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@
22

33
## 题目描述
44

5-
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
5+
<p>给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。</p>
66

7-
返回删除后的链表的头节点。
7+
<p>返回删除后的链表的头节点。</p>
88

9-
注意:此题对比[原题](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README.md)有改动。
9+
<p><strong>注意:</strong>此题对比原题有改动</p>
1010

11-
**示例 1:**
11+
<p><strong>示例 1:</strong></p>
1212

13-
```
14-
输入: head = [4,5,1,9], val = 5
15-
输出: [4,1,9]
16-
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
17-
```
13+
<pre><strong>输入:</strong> head = [4,5,1,9], val = 5
14+
<strong>输出:</strong> [4,1,9]
15+
<strong>解释: </strong>给定你链表中值为&nbsp;5&nbsp;的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -&gt; 1 -&gt; 9.
16+
</pre>
1817

19-
**示例 2:**
18+
<p><strong>示例 2:</strong></p>
2019

21-
```
22-
输入: head = [4,5,1,9], val = 1
23-
输出: [4,5,9]
24-
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
25-
```
20+
<pre><strong>输入:</strong> head = [4,5,1,9], val = 1
21+
<strong>输出:</strong> [4,5,9]
22+
<strong>解释: </strong>给定你链表中值为&nbsp;1&nbsp;的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -&gt; 5 -&gt; 9.
23+
</pre>
24+
25+
<p>&nbsp;</p>
2626

27-
**说明:**
27+
<p><strong>说明:</strong></p>
2828

29-
- 题目保证链表中节点的值互不相同
30-
- 若使用 C 或 C++ 语言,你不需要 `free``delete` 被删除的节点
29+
<ul>
30+
<li>题目保证链表中节点的值互不相同</li>
31+
<li>若使用 C 或 C++ 语言,你不需要 <code>free</code> 或 <code>delete</code> 被删除的节点</li>
32+
</ul>
3133

3234
## 解法
3335

@@ -47,9 +49,7 @@
4749
# self.next = None
4850
class Solution:
4951
def deleteNode(self, head: ListNode, val: int) -> ListNode:
50-
dummy = ListNode(0)
51-
dummy.next = head
52-
pre = dummy
52+
pre = dummy = ListNode(next=head)
5353
while pre.next and pre.next.val != val:
5454
pre = pre.next
5555
pre.next = None if not pre.next else pre.next.next
@@ -68,17 +68,16 @@ class Solution:
6868
* }
6969
*/
7070
class Solution {
71+
7172
public ListNode deleteNode(ListNode head, int val) {
72-
ListNode dummy = new ListNode(0);
73-
dummy.next = head;
73+
ListNode dummy = new ListNode(0, head);
7474
ListNode pre = dummy;
75-
while (pre.next != null && pre.next.val != val) {
76-
pre = pre.next;
77-
}
75+
for (; pre.next != null && pre.next.val != val; pre = pre.next);
7876
pre.next = pre.next == null ? null : pre.next.next;
7977
return dummy.next;
8078
}
8179
}
80+
8281
```
8382

8483
### **JavaScript**
@@ -97,36 +96,33 @@ class Solution {
9796
* @return {ListNode}
9897
*/
9998
var deleteNode = function (head, val) {
100-
const dummy = new ListNode(0);
101-
dummy.next = head;
99+
const dummy = new ListNode(0, head);
102100
let pre = dummy;
103-
while (pre.next && pre.next.val != val) {
104-
pre = pre.next;
105-
}
106-
pre.next = pre.next ? pre.next.next : null;
101+
for (; pre.next && pre.next.val != val; pre = pre.next);
102+
pre.next = pre.next?.next;
107103
return dummy.next;
108104
};
109105
```
110106
111107
### **Go**
112108
113109
```go
110+
/**
111+
* Definition for singly-linked list.
112+
* type ListNode struct {
113+
* Val int
114+
* Next *ListNode
115+
* }
116+
*/
114117
func deleteNode(head *ListNode, val int) *ListNode {
115-
res := &ListNode{
116-
Val: 0,
117-
Next: head,
118-
}
119-
pre := res
120-
cur := res.Next
121-
for cur != nil {
122-
if cur.Val == val {
123-
pre.Next = cur.Next
124-
return res.Next
125-
}
126-
cur = cur.Next
127-
pre = pre.Next
128-
}
129-
return res.Next
118+
dummy := &ListNode{0, head}
119+
pre := dummy
120+
for ; pre.Next != nil && pre.Next.Val != val; pre = pre.Next {
121+
}
122+
if pre.Next != nil {
123+
pre.Next = pre.Next.Next
124+
}
125+
return dummy.Next
130126
}
131127
```
132128
@@ -143,28 +139,12 @@ func deleteNode(head *ListNode, val int) *ListNode {
143139
*/
144140
class Solution {
145141
public:
146-
ListNode* deleteNode(ListNode* head, int val) {
147-
ListNode* cur = head;
148-
if (!head) {
149-
return nullptr;
150-
}
151-
152-
if (head->val == val) {
153-
// 第一个就匹配的情况
154-
return head->next;
155-
}
156-
157-
while (cur && cur->next) {
158-
if (cur->next->val == val) {
159-
// 如果找到了,直接指向下一个
160-
cur->next = cur->next->next;
161-
break;
162-
} else {
163-
cur = cur->next;
164-
}
165-
}
166-
167-
return head;
142+
ListNode *deleteNode(ListNode *head, int val) {
143+
ListNode *dummy = new ListNode(0, head);
144+
ListNode *pre = dummy;
145+
for (; pre->next && pre->next->val != val; pre = pre->next);
146+
pre->next = pre->next ? pre->next->next : nullptr;
147+
return dummy->next;
168148
}
169149
};
170150
```

lcof/面试题18. 删除链表的节点/Solution.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,11 @@
88
*/
99
class Solution {
1010
public:
11-
ListNode* deleteNode(ListNode* head, int val) {
12-
ListNode* cur = head;
13-
if (!head) {
14-
return nullptr;
15-
}
16-
17-
if (head->val == val) {
18-
// 第一个就匹配的情况
19-
return head->next;
20-
}
21-
22-
while (cur && cur->next) {
23-
if (cur->next->val == val) {
24-
// 如果找到了,直接指向下一个
25-
cur->next = cur->next->next;
26-
break;
27-
} else {
28-
cur = cur->next;
29-
}
30-
}
31-
32-
return head;
11+
ListNode *deleteNode(ListNode *head, int val) {
12+
ListNode *dummy = new ListNode(0, head);
13+
ListNode *pre = dummy;
14+
for (; pre->next && pre->next->val != val; pre = pre->next);
15+
pre->next = pre->next ? pre->next->next : nullptr;
16+
return dummy->next;
3317
}
3418
};
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
18
func deleteNode(head *ListNode, val int) *ListNode {
2-
res := &ListNode{
3-
Val: 0,
4-
Next: head,
5-
}
6-
pre := res
7-
cur := res.Next
8-
for cur != nil {
9-
if cur.Val == val {
10-
pre.Next = cur.Next
11-
return res.Next
12-
}
13-
cur = cur.Next
14-
pre = pre.Next
15-
}
16-
return res.Next
9+
dummy := &ListNode{0, head}
10+
pre := dummy
11+
for ; pre.Next != nil && pre.Next.Val != val; pre = pre.Next {
12+
}
13+
if pre.Next != nil {
14+
pre.Next = pre.Next.Next
15+
}
16+
return dummy.Next
1717
}

lcof/面试题18. 删除链表的节点/Solution.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
*/
99
class Solution {
1010
public ListNode deleteNode(ListNode head, int val) {
11-
ListNode dummy = new ListNode(0);
12-
dummy.next = head;
11+
ListNode dummy = new ListNode(0, head);
1312
ListNode pre = dummy;
14-
while (pre.next != null && pre.next.val != val) {
15-
pre = pre.next;
16-
}
13+
for (; pre.next != null && pre.next.val != val; pre = pre.next);
1714
pre.next = pre.next == null ? null : pre.next.next;
1815
return dummy.next;
1916
}

lcof/面试题18. 删除链表的节点/Solution.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
* @return {ListNode}
1212
*/
1313
var deleteNode = function (head, val) {
14-
const dummy = new ListNode(0);
15-
dummy.next = head;
14+
const dummy = new ListNode(0, head);
1615
let pre = dummy;
17-
while (pre.next && pre.next.val != val) {
18-
pre = pre.next;
19-
}
20-
pre.next = pre.next ? pre.next.next : null;
16+
for (; pre.next && pre.next.val != val; pre = pre.next);
17+
pre.next = pre.next?.next;
2118
return dummy.next;
2219
};

lcof/面试题18. 删除链表的节点/Solution.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
# self.next = None
66
class Solution:
77
def deleteNode(self, head: ListNode, val: int) -> ListNode:
8-
dummy = ListNode(0)
9-
dummy.next = head
10-
pre = dummy
8+
pre = dummy = ListNode(next=head)
119
while pre.next and pre.next.val != val:
1210
pre = pre.next
1311
pre.next = None if not pre.next else pre.next.next

0 commit comments

Comments
 (0)