Skip to content

237. Delete Node in a Linked List 👎

  • Time: $O(1)$
  • Space: $O(1)$
1
2
3
4
5
6
7
class Solution {
 public:
  void deleteNode(ListNode* node) {
    node->val = node->next->val;
    node->next = node->next->next;
  }
};
1
2
3
4
5
6
class Solution {
  public void deleteNode(ListNode node) {
    node.val = node.next.val;
    node.next = node.next.next;
  }
}
1
2
3
4
class Solution:
  def deleteNode(self, node):
    node.val = node.next.val
    node.next = node.next.next