-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (22 loc) · 941 Bytes
/
Solution.java
File metadata and controls
29 lines (22 loc) · 941 Bytes
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
// 237. Delete Node in a Linked List
// https://leetcode.com/problems/delete-node-in-a-linked-list/description/
// 时间复杂度: O(1)
// 空间复杂度: O(1)
public class Solution {
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 Solution()).deleteNode(node2);
System.out.println(head);
}
}