From 3f8fc0a74bc976ad515461aa6abe495251197009 Mon Sep 17 00:00:00 2001 From: Masakuni Date: Wed, 24 Dec 2025 22:59:59 +0900 Subject: [PATCH 1/3] step1 --- .../memo.md | 5 ++++ .../step1.py | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 problems/83.remove-duplicates-from-sorted-list/memo.md create mode 100644 problems/83.remove-duplicates-from-sorted-list/step1.py diff --git a/problems/83.remove-duplicates-from-sorted-list/memo.md b/problems/83.remove-duplicates-from-sorted-list/memo.md new file mode 100644 index 0000000..9f25e60 --- /dev/null +++ b/problems/83.remove-duplicates-from-sorted-list/memo.md @@ -0,0 +1,5 @@ +## step1 +何を返せばいい?先頭? +同じNodeで行き先が2つあることとかあるのか? +chatGPTで聞いたら今回はvalで判定するみたい +=> 141や142ではhead = [1,1,2,3,3]はNodeの行き先(next)を表していたのに一貫性がないように感じる \ No newline at end of file diff --git a/problems/83.remove-duplicates-from-sorted-list/step1.py b/problems/83.remove-duplicates-from-sorted-list/step1.py new file mode 100644 index 0000000..2207051 --- /dev/null +++ b/problems/83.remove-duplicates-from-sorted-list/step1.py @@ -0,0 +1,25 @@ +# +# @lc app=leetcode id=83 lang=python3 +# +# [83] Remove Duplicates from Sorted List +# + +# @lc code=start +# 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 and current.next: + if current.val == current.next.val: + current.next = current.next.next + else: + current = current.next + + return head + + +# @lc code=end From 5b246a89756a8a0eab763a72996e92af72ca80c6 Mon Sep 17 00:00:00 2001 From: Masakuni Date: Wed, 24 Dec 2025 22:59:59 +0900 Subject: [PATCH 2/3] step2 --- .../memo.md | 14 +++++++++- .../step2.py | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 problems/83.remove-duplicates-from-sorted-list/step2.py diff --git a/problems/83.remove-duplicates-from-sorted-list/memo.md b/problems/83.remove-duplicates-from-sorted-list/memo.md index 9f25e60..92aff19 100644 --- a/problems/83.remove-duplicates-from-sorted-list/memo.md +++ b/problems/83.remove-duplicates-from-sorted-list/memo.md @@ -2,4 +2,16 @@ 何を返せばいい?先頭? 同じNodeで行き先が2つあることとかあるのか? chatGPTで聞いたら今回はvalで判定するみたい -=> 141や142ではhead = [1,1,2,3,3]はNodeの行き先(next)を表していたのに一貫性がないように感じる \ No newline at end of file +=> 141や142ではhead = [1,1,2,3,3]はNodeの行き先(next)を表していたのに一貫性がないように感じる + +step2 +currentのvalとcurrent.nextのvalを比較していきたいが、current.nextがNoneの時にNone.valでエラーが生じるためどこでcurrent.nextがNoneでないかを判定するか迷った +step1ではwhileの中でcurrent.nextがNoneでないことを確認した +=> headがListNodeの型の場合`while current.next:`と書けたと思っている + +currentは他の人もよく書いているが、情報がないとの指摘もある +代わりにnodeを使っている人が多い印象 +previousを使う場合はcurrentにするのが意図が伝わりやすそう +その場合でもpreviousNodeとか書いたほうが親切かも + +他の方のコメントも確認したが概ね差がないように思える \ No newline at end of file diff --git a/problems/83.remove-duplicates-from-sorted-list/step2.py b/problems/83.remove-duplicates-from-sorted-list/step2.py new file mode 100644 index 0000000..108ec10 --- /dev/null +++ b/problems/83.remove-duplicates-from-sorted-list/step2.py @@ -0,0 +1,26 @@ +# +# @lc app=leetcode id=83 lang=python3 +# +# [83] Remove Duplicates from Sorted List +# + +# @lc code=start +# 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]: + node = head + + while node and node.next: + if node.val == node.next.val: + node.next = node.next.next + else: + node = node.next + + return head + + +# @lc code=end From 769ff271c5cdb32de83c58d58cc47ae4ef3c5983 Mon Sep 17 00:00:00 2001 From: Masakuni Date: Wed, 24 Dec 2025 22:59:59 +0900 Subject: [PATCH 3/3] step3 --- .../memo.md | 9 +++++-- .../step3.py | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 problems/83.remove-duplicates-from-sorted-list/step3.py diff --git a/problems/83.remove-duplicates-from-sorted-list/memo.md b/problems/83.remove-duplicates-from-sorted-list/memo.md index 92aff19..896fc8d 100644 --- a/problems/83.remove-duplicates-from-sorted-list/memo.md +++ b/problems/83.remove-duplicates-from-sorted-list/memo.md @@ -4,7 +4,7 @@ chatGPTで聞いたら今回はvalで判定するみたい => 141や142ではhead = [1,1,2,3,3]はNodeの行き先(next)を表していたのに一貫性がないように感じる -step2 +## step2 currentのvalとcurrent.nextのvalを比較していきたいが、current.nextがNoneの時にNone.valでエラーが生じるためどこでcurrent.nextがNoneでないかを判定するか迷った step1ではwhileの中でcurrent.nextがNoneでないことを確認した => headがListNodeの型の場合`while current.next:`と書けたと思っている @@ -14,4 +14,9 @@ currentは他の人もよく書いているが、情報がないとの指摘も previousを使う場合はcurrentにするのが意図が伝わりやすそう その場合でもpreviousNodeとか書いたほうが親切かも -他の方のコメントも確認したが概ね差がないように思える \ No newline at end of file +他の方のコメントも確認したが概ね差がないように思える + +## step3 +3回書いている途中に`node.next = node.next.next`の部分が連結先のnodeの先を付け替えているイメージが濃くなった気がする +現在のnodeは固定して条件を見て次のnodeを付け替えるor次のnodeに処理を進めるイメージを頭で浮かべながら書いた +元々解く時には都度条件を考えながら書いていたが解法が自分の中で定着しつつあると頭の中のリソースに余裕ができたおかげかもしれない \ No newline at end of file diff --git a/problems/83.remove-duplicates-from-sorted-list/step3.py b/problems/83.remove-duplicates-from-sorted-list/step3.py new file mode 100644 index 0000000..108ec10 --- /dev/null +++ b/problems/83.remove-duplicates-from-sorted-list/step3.py @@ -0,0 +1,26 @@ +# +# @lc app=leetcode id=83 lang=python3 +# +# [83] Remove Duplicates from Sorted List +# + +# @lc code=start +# 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]: + node = head + + while node and node.next: + if node.val == node.next.val: + node.next = node.next.next + else: + node = node.next + + return head + + +# @lc code=end