From 5e104d06b5b0155dda613e224b93c100e171f7fa Mon Sep 17 00:00:00 2001 From: wanwan87 Date: Tue, 21 Apr 2026 23:09:06 +0900 Subject: [PATCH 1/2] Create 83. Remove Duplicates from Sorted List.md --- 83. Remove Duplicates from Sorted List.md | 108 ++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 83. Remove Duplicates from Sorted List.md diff --git a/83. Remove Duplicates from Sorted List.md b/83. Remove Duplicates from Sorted List.md new file mode 100644 index 0000000..5428884 --- /dev/null +++ b/83. Remove Duplicates from Sorted List.md @@ -0,0 +1,108 @@ +# step1 +重複要素を削除するという文言だけ読むとsetを使いたくなるが、Listnodeはiterableじゃないので使えない。 + +``` +TypeError: 'ListNode' object is not iterable + a = set(head) +``` + +head = [1,1,2]の場合 +outputは[1,2] + +なのとソートされているので、重複している場合、同じ数字が連続で入ってくる。 + +if head.val == head.nextのとき,head.nextを削除する感じ? + +```python +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + current = head + print(current.val) + while current is not None: + print(current.val) + if current.val == current.next: + print(current.nextを削除する) + current = current.next + return current +``` + +printが出ない。head.next.のvalか。 + +```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]: + current = head + print(current) + print(current.val) + while current is not None: + print(current.val) + if current.val == current.next.val: + print("current.nextを削除する") + current.next = current.next.next + current = current.next + return current +``` + +current.nextを削除するというよりも、listnodeは(1,next)->(1,next)->(2,next) +nextを2のところを指すようにするのかな + +currentはheadを参照していて、currentを操作するとheadのlistnodeも変更が加わる。なのでreturn + +```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]: + current = head + print(current) + print(current.val) + while current and current.next is not None: + print(current.val) + if current.val == current.next.val: + print("current.nextを削除する") + current.next = current.next.next + current = current.next + return head +``` + +[1,1,1]のときに通らない。出力を見ると、if文に1回しか入っていない。 + +[1(A),1(B),1(C)]としたときに、 + +if文の中はcurrent.next(B) = current.next(C) + +出た後に + +current = current.next(C) + +で次のループでcurrentが(C)でcurrent.nextがNoneになってしまって終わっている。なので条件に合致しない場合のみcurrent = current.nextで進めるようにする + +```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]: + current = head + while current is not None and current.next is not None: + print(current.val) + if current.val == current.next.val: + print("a") + current.next = current.next.next + else: + current = current.next + return head +``` + +# step2 +PRを見ていくがいったん休憩 From 73a926880f9990f596285081a708c24ff087396b Mon Sep 17 00:00:00 2001 From: wanwan87 Date: Thu, 23 Apr 2026 20:18:21 +0900 Subject: [PATCH 2/2] Update 83. Remove Duplicates from Sorted List.md --- 83. Remove Duplicates from Sorted List.md | 65 ++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/83. Remove Duplicates from Sorted List.md b/83. Remove Duplicates from Sorted List.md index 5428884..0fa4fb7 100644 --- a/83. Remove Duplicates from Sorted List.md +++ b/83. Remove Duplicates from Sorted List.md @@ -105,4 +105,67 @@ class Solution: ``` # step2 -PRを見ていくがいったん休憩 +PRを見ていく +いくつかコメントももらっているので、修正していく + +- 変数名currentの情報量がすくないのでnodeにする + - 情報量を増やすならcurrent_nodeも良さげだが長ければ良いわけではない + - https://discord.com/channels/1084280443945353267/1200089668901937312/1207200647594639391 + - 命名の感覚はこのあたりもhttps://discord.com/channels/1084280443945353267/1200089668901937312/1209882689411223644 + - 関数内の主役なら1,2単語、ループを出ても使うような変数名は分かりやすい名前を付けてあげる + - 日本語でやる場合はどうつけるか +- デバッグ用print文は消す +- valueとnodeの比較をしている箇所 + - 中身を理解せずにやりたいことが先行してしまってそのまま書いちゃってるのかもしれない +- 再帰で呼び出す方法 + - https://github.com/attractal/leetcode/pull/3/changes + - 再帰の深さの限界などの制約もあるので、使いどころは見極める必要がありそう + +# step3 + +考えずに書くと最後のreturnをnodeにしそう。nodeはwhileの中で操作しているので最後のnodeの値が入っている({val:2,next: None}のような)。 + +node is not Noneはnodeが空のときのための条件。whileの前に書いてもいいかも。 + +```python +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + node = head + while node is not None and node.next is not None: + if node.val == node.next.val: + node.next = node.next.next + else: + node = node.next + return head +``` + +if node is Noneを先に書く方法を試したときにif文のnode.valにするのを忘れた。脳のリソースがそっちに割かれたのかもしれない。 + +```python +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + node = head + + if node is None: + return head + while node.next is not None: + if node.val == node.next.val: + node.next = node.next.next + else: + node = node.next + return head +``` + +```python +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + node = head + if node is None: + return None + while node.next is not None: + if node.val == node.next.val: + node.next = node.next.next + else: + node = node.next + return head +```