Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions problems/83.remove-duplicates-from-sorted-list/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## step1
何を返せばいい?先頭?
同じNodeで行き先が2つあることとかあるのか?
chatGPTで聞いたら今回はvalで判定するみたい
=> 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は他の人もよく書いているが、情報がないとの指摘もある
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ほかの方のソースコードを読んだ際、そのコードのリンクと読んだときの所感をプルリクエストに添えると、レビューワーにとって有益な情報となると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
step2で他の方のソースコードを確認するので次回以降、読んだソースコードと所感をmemo欄に付け加えるようにいたします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

うーん。current は私がかなり嫌なんですね。

名前からなんらかの役割やコンテキストが推測できて欲しいのに、current は「自分が今注目しているもの」という程度の意味合いしかここではないので、まったく読む人にとってありがたくないのです。意味を乗せる気がないならば一文字変数のほうがましです。意味を乗せる気がない捨てていい変数だという意味が乗るからです。

逆に、current をプロダクションレベルのコードで使うときには、「現在の設定やスレッド」などのようにグローバルな状態に近い意味で使うことが多いです。

代わりにnodeを使っている人が多い印象
previousを使う場合はcurrentにするのが意図が伝わりやすそう
その場合でもpreviousNodeとか書いたほうが親切かも

他の方のコメントも確認したが概ね差がないように思える

## step3
3回書いている途中に`node.next = node.next.next`の部分が連結先のnodeの先を付け替えているイメージが濃くなった気がする
現在のnodeは固定して条件を見て次のnodeを付け替えるor次のnodeに処理を進めるイメージを頭で浮かべながら書いた
元々解く時には都度条件を考えながら書いていたが解法が自分の中で定着しつつあると頭の中のリソースに余裕ができたおかげかもしれない
25 changes: 25 additions & 0 deletions problems/83.remove-duplicates-from-sorted-list/step1.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions problems/83.remove-duplicates-from-sorted-list/step2.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions problems/83.remove-duplicates-from-sorted-list/step3.py
Original file line number Diff line number Diff line change
@@ -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