Delete Node in a Linked List
LeetCode 237 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু
Copy next's value, then skip next.
আমি Delete Node in a Linked List (LeetCode 237) এভাবে সমাধান করেছি — Basics প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।
নিচের বিবরণ ইংরেজিতে।
Intuition
You only get the node to delete, not its predecessor, so you can't unlink it. Instead, copy the next node's value into it and unlink the next node — the list now looks as if this node was removed.
Approach
- 1
temp = node.next.
- 2
node.val = temp.val.
- 3
node.next = node.next.next.
আমি কীভাবে সমাধান করেছি
Copy and skipPythonO(1)
class Solution:
def deleteNode(self, node):
temp = node.next
node.val = temp.val
node.next = node.next.nextDelete Node in a Linked List — সাধারণ প্রশ্ন
Delete Node in a Linked List-এর পেছনে intuition কী?
You only get the node to delete, not its predecessor, so you can't unlink it. Instead, copy the next node's value into it and unlink the next node — the list now looks as if this node was removed.
Delete Node in a Linked List (LeetCode 237) কীভাবে সমাধান করবেন?
1. temp = node.next. 2. node.val = temp.val. 3. node.next = node.next.next.
Delete Node in a Linked List-এর time আর space complexity কত?
Time: O(1)। Space: O(1)। (Accepted approach: Copy and skip।)
Delete Node in a Linked List-এ সাধারণ ভুলগুলো কী?
Works only because the node is never the tail.