-
Notifications
You must be signed in to change notification settings - Fork 0
83. Remove Duplicates from Sorted List #2
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
katataku
wants to merge
1
commit into
main
Choose a base branch
from
83.-Remove-Duplicates-from-Sorted-List
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
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,82 @@ | ||
|
||
URL: https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/ | ||
|
||
# Step 1 | ||
|
||
- 実装時間: 3分 | ||
- 時間計算量: O(n) | ||
- 空間計算量: O(1) | ||
- while条件とif条件が似ているのが気になったけど、これ以外の実装は思い付かなかったのでsubmit. | ||
|
||
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
current = head | ||
while current is not None: | ||
if current.next is None: | ||
return head | ||
if current.val == current.next.val: | ||
current.next = current.next.next | ||
else: | ||
current = current.next | ||
return head | ||
``` | ||
|
||
# Step 2 | ||
|
||
- 過去のPRを見ていると、再帰Verの話があったので、僕も実装してみる。 | ||
|
||
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if head is None: | ||
return head | ||
current = head | ||
while current.next is not None and current.val == current.next.val: | ||
current = current.next | ||
current.next = self.deleteDuplicates(current.next) | ||
return current | ||
``` | ||
|
||
- 過去のPRにて、末尾再帰最適化の議論を見た。 | ||
- 今回は`current.next`を更新しているので、単純に末尾再帰は出来なさそう。 | ||
- そもそもPythonでの末尾再帰の実装とか条件を知らないことに気づいたので調べてみる。→Pythonにはない。 | ||
https://www.python.jp/news/wnpython311/inline-function.html | ||
|
||
|
||
- Discordを見ていて`current`という変数の命名について無意識についけていたことに気づいた。別の候補として`current_node`や`current_focusing_node`を思いついた。しかし、今回は短い処理であり、今注目しているノードという点は伝わる範囲だと思うので、`current`という変数名を使うことにする。もっと長い処理場合は、より意味のこもった変数名にする。 | ||
|
||
- 過去のコメントより、ifの判定はwhile条件に入れるだけでいいことを理解した。↓改良版。 | ||
|
||
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
current = head | ||
while current is not None and current.next is not None: | ||
if current.val == current.next.val: | ||
current.next = current.next.next | ||
else: | ||
current = current.next | ||
return head | ||
``` | ||
|
||
- `current is not None`をループごとに確認するのは冗長だと気づいた。関数の初めに一度確認すれば、あとは確認不要。 | ||
|
||
# Step 3 | ||
|
||
- 時間計算量: O(n) | ||
- 空間計算量: O(1) | ||
|
||
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if head is None: | ||
return None | ||
current = head | ||
while current.next is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 正常系では、1回のループでノードが1つ進むとするとif文はcontinueして、elseを消すのもありかもしれないです。 while current.next is not None:
if current.val == current.next.val:
current.next = current.next.next
continue
current = current.next There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。 |
||
if current.val == current.next.val: | ||
current.next = current.next.next | ||
else: | ||
current = current.next | ||
return head | ||
``` |
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.
本当に上から読んでいって明らかならば node だけでもいいと思うんですよね。読み手に何を伝えたいか次第です。
for i in range(len(array)):
と書かれていたら、添字の配列であろうとほぼ確信して次にいけるわけです。これが長くても迷彩か擬態にしかなりません。
https://discord.com/channels/1084280443945353267/1200089668901937312/1209882689411223644
https://discord.com/channels/1084280443945353267/1225849404037009609/1234218057199784077