Tensorum
YouTubeশেখা শুরু করুন
LeetCode 82 · Basics

Remove Duplicates from Sorted List II

LeetCode 82 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Dummy node; skip whole duplicate runs.

মাঝারিLinked ListTwo Pointers
LeetCode-এ খুলুন ↗

আমি 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.