- "The given node will not be the tail and it will always be a valid node of the linked list" - Our trick below only works if the input node is not the tail of the list
- "The linked list will have at least two elements" - However, we still check for 2+ element lists in the code
public class ListNode {
int val;
ListNode next;
}
class Solution {
public void deleteNode(ListNode n) {
if (n == null || n.next == null) {
return;
}
n.val = n.next.val;
n.next = n.next.next;
}
}
- Time Complexity: O(1)
- Space Complexity: O(1)