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.