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.