From 36ac86b7f4d0c9c475a32badcc7b6230cff34183 Mon Sep 17 00:00:00 2001 From: TrsmYsk <53941356+TrsmYsk@users.noreply.github.com> Date: Fri, 3 Oct 2025 08:25:49 +0900 Subject: [PATCH] Create 82. Remove Duplicates from Sorted List II.md --- .... Remove Duplicates from Sorted List II.md | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 82. Remove Duplicates from Sorted List II/82. Remove Duplicates from Sorted List II.md diff --git a/82. Remove Duplicates from Sorted List II/82. Remove Duplicates from Sorted List II.md b/82. Remove Duplicates from Sorted List II/82. Remove Duplicates from Sorted List II.md new file mode 100644 index 0000000..a69ce00 --- /dev/null +++ b/82. Remove Duplicates from Sorted List II/82. Remove Duplicates from Sorted List II.md @@ -0,0 +1,97 @@ +問題文: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ + +# step1: 何も見ないで書く(制限時間5分) +- 時間計算量O(n)、空間計算量O(1) +- 新井さんの解説を丸写しした。正直コードは読めていない。 +- 小田さんが言うところの仕事の引継ぎという発想が欠けていることを痛感した。 +```python +# 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(next = head) + node = dummy + while node.next is not None and node.next.next is not None: + if node.next.val == node.next.next.val: + copy = node.next + while copy.next is not None and copy.next.val == copy.val: + copy.next = copy.next.next + node.next = copy.next + else: + node = node.next + return dummy.next + +``` +# step2: コードを整える +## 2-1 +- 毎回のループで引き継ぐもの + 1. 重複無しのノード: このグループの末尾を`latest_unique`で保持。 + 2. 重複未チェックのノード: このグループの先頭のノードは`latest_unique.next`。これがループの主役なので`checking`と名付けて保持。 +- skip_duplicates()に入力エラー処理が必要か迷ったが、`None`が入力されることはないのでとりあえずこのまま。 +```python +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + def skip_duplicates(node): + while node.next is not None and node.next.val == node.val: + node.next = node.next.next + return node.next + dummy = ListNode(next = head) + latest_unique = dummy + checking = latest_unique.next + while checking is not None and checking.next is not None: + if checking.val != checking.next.val: + latest_unique = checking + else: + latest_unique.next = skip_duplicates(checking) + checking = latest_unique.next + return dummy.next + +``` +## 2-2 +- 2-1のコードをさらに整理。参考にした議論はdiscordのリンクを保存してなかったので引用できないが、小田さんがいろいろ突っ込みを入れていて19個目のつっこみで条件式を色々と変形していたあたりを参考にした。 +- こっちのほうが読みやすい。ネストが浅いし、重複を削除するときとしないときでは違う処理をしないといけないが、それらが違うブロックにまとまっている。 +```python +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + def skip_duplicates(node): + while node.next is not None and node.next.val == node.val: + node.next = node.next.next + return node.next + dummy = ListNode(next = head) + latest_unique = dummy + checking = latest_unique.next + while checking is not None and checking.next is not None: + if checking.val != checking.next.val: + latest_unique = checking + checking = latest_unique.next + continue + latest_unique.next = skip_duplicates(checking) + checking = latest_unique.next + return dummy.next + +``` + +# step3: ミスなく3回書く(制限時間10分) +```python +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + def skip_duplicates(node): + while node.next is not None and node.next.val == node.val: + node.next = node.next.next + return node.next + dummy = ListNode(next = head) + latest_unique = dummy + checking = latest_unique.next + while checking is not None and checking.next is not None: + if checking.val != checking.next.val: + latest_unique = checking + checking = latest_unique.next + continue + latest_unique.next = skip_duplicates(checking) + checking = latest_unique.next + return dummy.next + +```