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.