LeetCode - Algorithms - 237. Delete Node in a Linked List

Java

How do I delete a node in a linked list?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
ListNode temp = node.next;
node.val = temp.val;
node.next = temp.next;
temp = null;
}
}

Submission Detail

  • 41 / 41 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Delete Node in a Linked List.
  • Memory Usage: 37.6 MB, less than 100.00% of Java online submissions for Delete Node in a Linked List.