forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode237.java
More file actions
38 lines (28 loc) · 1.3 KB
/
LeetCode237.java
File metadata and controls
38 lines (28 loc) · 1.3 KB
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
package LeetCode;
public class LeetCode237 {
// 237. Delete Node in a Linked List
// https://leetcode.com/problems/delete-node-in-a-linked-list/description/
// 时间复杂度: O(1)
// 空间复杂度: O(1)
// 这里只给出链表的值让我们删除--->一般我们删除链表的节点需要找到对应的前一个节点
// 由于这里仅仅是值,所以我们可以这样做:
// 1. 找到给定值的节点
// 2. 将找到的节点的下一个节点的值赋值给当前节点
// 3. 删除掉下一个节点
public void deleteNode(ListNode node) {
// 注意: 这个方法对尾节点不适用。题目中要求了给定的node不是尾节点
// 我们检查node.next, 如果为null则抛出异常, 确保了node不是尾节点
if (node == null || node.next == null)
throw new IllegalArgumentException("node should be valid and can not be the tail node.");
node.val = node.next.val;
node.next = node.next.next;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
ListNode head = new ListNode(arr);
System.out.println(head);
ListNode node2 = head.findNode(2);
(new LeetCode237()).deleteNode(node2);
System.out.println(head);
}
}