Post

Delete Node in a Linked List

LeetCode https://leetcode.cn/problems/delete-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) {

        node.val = node.next.val;
        node.next = node.next.next;
    }
}

Complexity

  • Time = O(1)
  • Space = O(1)

This post is licensed under CC BY 4.0 by the author.