This one is a basic linked list problem. Here is the problem link. See the detailed explanation there.
This problem is very simple and straightforward. We have to delete the given node.
We don’t need to check anything and don’t need to return anything. See the note in the below of the problem explanation.
Suppose we have a linked list something like that, 1->2->3->4->5
. The function provides the last node for delete. After the deletion, the linked list will be 1->2->3->4
.
To do this, we will just store the next node value of our given node in the given node value. Will also store the reference in the same way.
Let’s see the code for better understanding.
class Solution: def deleteNode(self, node): node.val = node.next.val node.next = node.next.next
We can do this in one line.
class Solution: def deleteNode(self, node): node.val, node.next = node.next.val, node.next.next
If you submit the code in LeetCode, it will be accepted.