DEV Community

Giuseppe
Giuseppe

Posted on

LeetCode #237. Delete node in a LinkedList.

Time Complexity: O(1)

Copy the value from the next node: node.val = node.next.val
Update the pointer to skip the next node: node.next = node.next.next

No loops, no recursion, no operations that depend on the size of the linked list. It always takes the same amount of time.

Space Complexity: O(1)

class Solution {
    public void deleteNode(ListNode node) {

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

    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)