Skip to content

Commit a4986fb

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 02.03. 删除中间节点
1 parent 6ef878b commit a4986fb

File tree

4 files changed

+127
-37
lines changed

4 files changed

+127
-37
lines changed

lcci/02.03.Delete Middle Node/README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,46 @@
1616

1717
## 解法
1818
<!-- 这里可写通用的实现逻辑 -->
19-
19+
把 node 的下一个节点的值赋给 node,然后改变 node 的 next 指向。
2020

2121
### Python3
2222
<!-- 这里可写当前语言的特殊实现逻辑 -->
2323

2424
```python
25+
# Definition for singly-linked list.
26+
# class ListNode:
27+
# def __init__(self, x):
28+
# self.val = x
29+
# self.next = None
2530

31+
class Solution:
32+
def deleteNode(self, node):
33+
"""
34+
:type node: ListNode
35+
:rtype: void Do not return anything, modify node in-place instead.
36+
"""
37+
node.val = node.next.val
38+
node.next = node.next.next
2639
```
2740

2841
### Java
2942
<!-- 这里可写当前语言的特殊实现逻辑 -->
3043

3144
```java
32-
45+
/**
46+
* Definition for singly-linked list.
47+
* public class ListNode {
48+
* int val;
49+
* ListNode next;
50+
* ListNode(int x) { val = x; }
51+
* }
52+
*/
53+
class Solution {
54+
public void deleteNode(ListNode node) {
55+
node.val = node.next.val;
56+
node.next = node.next.next;
57+
}
58+
}
3359
```
3460

3561
### ...
+71-35
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,71 @@
1-
# [02.03. Delete Middle Node](https://leetcode-cn.com/problems/delete-middle-node-lcci)
2-
3-
## Description
4-
<p>Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node.</p>
5-
6-
<p>&nbsp;</p>
7-
8-
<p><strong>Example: </strong></p>
9-
10-
<pre>
11-
<strong>Input: </strong>the node c from the linked list a-&gt;b-&gt;c-&gt;d-&gt;e-&gt;f
12-
<strong>Output: </strong>nothing is returned, but the new linked list looks like a-&gt;b-&gt;d-&gt;e-&gt;f
13-
</pre>
14-
15-
16-
17-
## Solutions
18-
19-
20-
### Python3
21-
22-
```python
23-
24-
```
25-
26-
### Java
27-
28-
```java
29-
30-
```
31-
32-
### ...
33-
```
34-
35-
```
1+
# [02.03. Delete Middle Node](https://leetcode-cn.com/problems/delete-middle-node-lcci)
2+
3+
## Description
4+
<p>Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node.</p>
5+
6+
7+
8+
<p>&nbsp;</p>
9+
10+
11+
12+
<p><strong>Example: </strong></p>
13+
14+
15+
16+
<pre>
17+
18+
<strong>Input: </strong>the node c from the linked list a-&gt;b-&gt;c-&gt;d-&gt;e-&gt;f
19+
20+
<strong>Output: </strong>nothing is returned, but the new linked list looks like a-&gt;b-&gt;d-&gt;e-&gt;f
21+
22+
</pre>
23+
24+
25+
26+
27+
## Solutions
28+
29+
30+
### Python3
31+
32+
```python
33+
# Definition for singly-linked list.
34+
# class ListNode:
35+
# def __init__(self, x):
36+
# self.val = x
37+
# self.next = None
38+
39+
class Solution:
40+
def deleteNode(self, node):
41+
"""
42+
:type node: ListNode
43+
:rtype: void Do not return anything, modify node in-place instead.
44+
"""
45+
node.val = node.next.val
46+
node.next = node.next.next
47+
```
48+
49+
### Java
50+
51+
```java
52+
/**
53+
* Definition for singly-linked list.
54+
* public class ListNode {
55+
* int val;
56+
* ListNode next;
57+
* ListNode(int x) { val = x; }
58+
* }
59+
*/
60+
class Solution {
61+
public void deleteNode(ListNode node) {
62+
node.val = node.next.val;
63+
node.next = node.next.next;
64+
}
65+
}
66+
```
67+
68+
### ...
69+
```
70+
71+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public void deleteNode(ListNode node) {
11+
node.val = node.next.val;
12+
node.next = node.next.next;
13+
}
14+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def deleteNode(self, node):
9+
"""
10+
:type node: ListNode
11+
:rtype: void Do not return anything, modify node in-place instead.
12+
"""
13+
node.val = node.next.val
14+
node.next = node.next.next

0 commit comments

Comments
 (0)