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.