-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0237.删除链表中的节点.java
65 lines (64 loc) · 1.55 KB
/
0237.删除链表中的节点.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* @lc app=leetcode.cn id=237 lang=java
*
* [237] 删除链表中的节点
*
* https://leetcode.cn/problems/delete-node-in-a-linked-list/description/
*
* algorithms
* Easy (85.91%)
* Likes: 1154
* Dislikes: 0
* Total Accepted: 298.4K
* Total Submissions: 347.2K
* Testcase Example: '[4,5,1,9]\n5'
*
* 请编写一个函数,用于 删除单链表中某个特定节点 。在设计函数时需要注意,你无法访问链表的头节点 head ,只能直接访问 要被删除的节点 。
*
* 题目数据保证需要删除的节点 不是末尾节点 。
*
*
*
* 示例 1:
*
*
* 输入:head = [4,5,1,9], node = 5
* 输出:[4,1,9]
* 解释:指定链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9
*
*
* 示例 2:
*
*
* 输入:head = [4,5,1,9], node = 1
* 输出:[4,5,9]
* 解释:指定链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9
*
*
*
* 提示:
*
*
* 链表中节点的数目范围是 [2, 1000]
* -1000 <= Node.val <= 1000
* 链表中每个节点的值都是 唯一 的
* 需要删除的节点 node 是 链表中的节点 ,且 不是末尾节点
*
*
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
// @lc code=end