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;
}
}
Top comments (0)