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.