Delete node in a linked list

October 4, 2022

linked-list

Problem URL: Delete node in a linked list

As we are not given with head node, we will copy the value of next node to the current node. Then we remove the next node by moving the next pointer of current node to the next pointer of the next node.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteNode(self, node):
        node.val = node.next.val
        node.next = node.next.next

Time Complexity: O(1)
Space Complexity: O(1)