Tensorum
YouTubeसीखना शुरू करें
LeetCode 82 · Basics

Remove Duplicates from Sorted List II

LeetCode 82 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Dummy node; skip whole duplicate runs.

मध्यमLinked ListTwo Pointers

मैंने Remove Duplicates from Sorted List II (LeetCode 82) ऐसे हल किया — Basics पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Every value that appears more than once must vanish completely. Use a dummy head and a tail pointer: when the current node starts a run of duplicates, skip the whole run; otherwise attach it to the result.

Approach

  1. 1

    dummy → temp is the tail of the kept list.

  2. 2

    If node and node.next share a value, advance to the last of the run and link temp past it.

  3. 3

    Otherwise link node and advance temp.

  4. 4

    Move node forward.

मैंने कैसे हल किया

Dummy head + run skippingPythonO(n) time · O(1) space
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(-1)
        temp = dummy
        node = head
        while node:
            if node.next and node.val == node.next.val:
                while node.next and node.val == node.next.val:
                    node = node.next
                temp.next = node.next
            else:
                temp.next = node
                temp = temp.next
            node = node.next
        return dummy.next
स्टेप-थ्रू लोड हो रहा है…

Remove Duplicates from Sorted List II — अक्सर पूछे जाने वाले प्रश्न

Remove Duplicates from Sorted List II के पीछे intuition क्या है?

Every value that appears more than once must vanish completely. Use a dummy head and a tail pointer: when the current node starts a run of duplicates, skip the whole run; otherwise attach it to the result.

Remove Duplicates from Sorted List II (LeetCode 82) कैसे हल करें?

1. dummy → temp is the tail of the kept list. 2. If node and node.next share a value, advance to the last of the run and link temp past it. 3. Otherwise link node and advance temp. 4. Move node forward.

Remove Duplicates from Sorted List II की time और space complexity क्या है?

Time: O(n)। Space: O(1)। (Accepted approach: Dummy head + run skipping।)

Remove Duplicates from Sorted List II में आम गलतियाँ क्या हैं?

The dummy head handles duplicates at the very start.