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.