Skip to content

Commit bca136c

Browse files
committed
add 237
1 parent 79fd168 commit bca136c

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
## Easy
1010

1111
以下是 LeetCode 官方精选的经典面试问题列表:
12-
[Top Interview Questions Easy Collection][easy]
12+
[Top Interview Questions Easy Collection][interview-easy]
1313

1414
**Array**
1515

@@ -41,6 +41,13 @@
4141
| 38 | [count-and-say][038] |
4242
| 14 | [Longest Common Prefix][014] |
4343

44+
**Linked List**
45+
46+
| # | Title |
47+
| :--: | :------------------------------------------ |
48+
| 237 | [Delete Node in a Linked List][237] |
49+
50+
4451

4552
**其他**
4653

@@ -52,7 +59,7 @@
5259

5360
[leetcode]: https://leetcode.com/problemset/all/
5461
[blankj]: https://github.com/Blankj/awesome-java-leetcode
55-
[easy]: https://leetcode.com/explore/interview/card/top-interview-questions-easy/
62+
[interview-easy]: https://leetcode.com/explore/interview/card/top-interview-questions-easy/
5663

5764
[026]: https://github.com/andavid/leetcode-java/blob/master/note/026/README.md
5865
[121]: https://github.com/andavid/leetcode-java/blob/master/note/121/README.md
@@ -75,3 +82,4 @@
7582
[028]: https://github.com/andavid/leetcode-java/blob/master/note/028/README.md
7683
[038]: https://github.com/andavid/leetcode-java/blob/master/note/038/README.md
7784
[014]: https://github.com/andavid/leetcode-java/blob/master/note/014/README.md
85+
[237]: https://github.com/andavid/leetcode-java/blob/master/note/237/README.md

note/237/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# [Delete Node in a Linked List][title]
2+
3+
## Description
4+
5+
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
6+
7+
Supposed the linked list is `1 -> 2 -> 3 -> 4` and you are given the third node with value `3`, the linked list should become `1 -> 2 -> 4` after calling your function.
8+
9+
## 思路
10+
11+
常规方法是修改该节点前面的那个节点。由于只能访问当前节点,且当前节点不是最末尾的节点,可以放心的把其 next 节点的值拷贝过来,并修改其 next 节点。
12+
13+
## [完整代码][src]
14+
15+
```java
16+
class Solution {
17+
public void deleteNode(ListNode node) {
18+
node.val = node.next.val;
19+
node.next = node.next.next;
20+
}
21+
}
22+
```
23+
24+
[title]: https://leetcode.com/problems/delete-node-in-a-linked-list
25+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_237/Solution.java
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
3+
public void deleteNode(ListNode node) {
4+
node.val = node.next.val;
5+
node.next = node.next.next;
6+
}
7+
8+
public class ListNode {
9+
int val;
10+
ListNode next;
11+
ListNode(int x) {
12+
val = x;
13+
}
14+
}
15+
16+
}

0 commit comments

Comments
 (0)