Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 237 · Basics

Delete Node in a Linked List

LeetCode 237 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Copy next's value, then skip next.

मध्यमLinked List

मी 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. 1

    temp = node.next.

  2. 2

    node.val = temp.val.

  3. 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.next
स्टेप-थ्रू लोड होत आहे…

Delete 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.