-
Notifications
You must be signed in to change notification settings - Fork 0
83. Remove Duplicates from Sorted List #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xbam326
wants to merge
3
commits into
main
Choose a base branch
from
83
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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は他の人もよく書いているが、情報がないとの指摘もある | ||
| 代わりにnodeを使っている人が多い印象 | ||
| previousを使う場合はcurrentにするのが意図が伝わりやすそう | ||
| その場合でもpreviousNodeとか書いたほうが親切かも | ||
|
|
||
| 他の方のコメントも確認したが概ね差がないように思える | ||
|
|
||
| ## step3 | ||
| 3回書いている途中に`node.next = node.next.next`の部分が連結先のnodeの先を付け替えているイメージが濃くなった気がする | ||
| 現在のnodeは固定して条件を見て次のnodeを付け替えるor次のnodeに処理を進めるイメージを頭で浮かべながら書いた | ||
| 元々解く時には都度条件を考えながら書いていたが解法が自分の中で定着しつつあると頭の中のリソースに余裕ができたおかげかもしれない | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ほかの方のソースコードを読んだ際、そのコードのリンクと読んだときの所感をプルリクエストに添えると、レビューワーにとって有益な情報となると思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
step2で他の方のソースコードを確認するので次回以降、読んだソースコードと所感をmemo欄に付け加えるようにいたします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
うーん。current は私がかなり嫌なんですね。
名前からなんらかの役割やコンテキストが推測できて欲しいのに、current は「自分が今注目しているもの」という程度の意味合いしかここではないので、まったく読む人にとってありがたくないのです。意味を乗せる気がないならば一文字変数のほうがましです。意味を乗せる気がない捨てていい変数だという意味が乗るからです。
逆に、current をプロダクションレベルのコードで使うときには、「現在の設定やスレッド」などのようにグローバルな状態に近い意味で使うことが多いです。