-
Notifications
You must be signed in to change notification settings - Fork 0
82. Remove Duplicates from Sorted List II #6
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
rinost081
wants to merge
2
commits into
main
Choose a base branch
from
LeetCode82
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
2 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,79 @@ | ||||||||||||||||||||||||||||||||
"""step1 | ||||||||||||||||||||||||||||||||
基本は83と同じ。 | ||||||||||||||||||||||||||||||||
83と同じ方針でやるなら時間計算量はO(n)かな。 | ||||||||||||||||||||||||||||||||
方針としては、node.nextを付け替える操作を繰り返すだけ。 | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
やりたいことは、 | ||||||||||||||||||||||||||||||||
1. 現在の値と次の値が同じかを比較 | ||||||||||||||||||||||||||||||||
2. a. 同じ場合はwhile文を使って、その次の値も同じかを繰り返し比較 | ||||||||||||||||||||||||||||||||
b. 違う場合は次のnodeに進み1に戻る。 | ||||||||||||||||||||||||||||||||
3. a. 繰り返して比較するため現在のnodeと連続する最後のnodeを特定 | ||||||||||||||||||||||||||||||||
4. a. 現在の一つ前のnode→最後のnodeの次へと付け替える | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
これを繰り返す。 | ||||||||||||||||||||||||||||||||
しかしprevが存在しないため1は現在の値と次の値ではなく、次の値と次の次の値を比較する。2以降もこれに対応するようにずらす。 | ||||||||||||||||||||||||||||||||
テストケース2みたいにheadを消さなければならないときはどうするんだ。最初の処理で時間を費やしたので2個目のwhile分は無限ループに入っている。 | ||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||
class Solution: | ||||||||||||||||||||||||||||||||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||||||||||||||||||||||||||||||||
node = head | ||||||||||||||||||||||||||||||||
end_node = head | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
while end_node.next: | ||||||||||||||||||||||||||||||||
if end_node.val == end_node.next.val: | ||||||||||||||||||||||||||||||||
end_node = end_node.next | ||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
head.next = end_node.next | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
while node.next and node.next.next: | ||||||||||||||||||||||||||||||||
if node.next.val == node.next.next.val: | ||||||||||||||||||||||||||||||||
end_node = node.next.next | ||||||||||||||||||||||||||||||||
while end_node.next: | ||||||||||||||||||||||||||||||||
if node.next.val == end_node.next: | ||||||||||||||||||||||||||||||||
end_node = end_node.next | ||||||||||||||||||||||||||||||||
node.next = end_node.next | ||||||||||||||||||||||||||||||||
node = node.next | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return head | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"""step2 | ||||||||||||||||||||||||||||||||
解答を見る感じ方針としては、大きく間違えているわけではなさそう。 | ||||||||||||||||||||||||||||||||
テストケース2みたいにheadを消さないといけない場合に備えてdummy nodeを作成すれば解決。 | ||||||||||||||||||||||||||||||||
@h1rosakaさんのプルリクをかなり参考にしました。 (https://github.com/h1rosaka/arai60/pull/6) | ||||||||||||||||||||||||||||||||
時間計算量は全てのnodeを1度みるためO(n) | ||||||||||||||||||||||||||||||||
空間計算量はdummy nodeを作成したときのO(1)→定数空間 | ||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||
class Solution: | ||||||||||||||||||||||||||||||||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||||||||||||||||||||||||||||||||
dummy = ListNode(-1, head) | ||||||||||||||||||||||||||||||||
node = dummy | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
while node: | ||||||||||||||||||||||||||||||||
if node.next and node.next.next and node.next.val == node.next.next.val: | ||||||||||||||||||||||||||||||||
while node.next and node.next.next and node.next.val == node.next.next.val: | ||||||||||||||||||||||||||||||||
node.next = node.next.next | ||||||||||||||||||||||||||||||||
node.next = node.next.next | ||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||
node = node.next | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return dummy.next | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
"""step3 | ||||||||||||||||||||||||||||||||
1分30秒, step2と同じ | ||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||
class Solution: | ||||||||||||||||||||||||||||||||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||||||||||||||||||||||||||||||||
dummy = ListNode(-1, head) | ||||||||||||||||||||||||||||||||
node = dummy | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
while node: | ||||||||||||||||||||||||||||||||
if node.next and node.next.next and node.next.val == node.next.next.val: | ||||||||||||||||||||||||||||||||
while node.next and node.next.next and node.next.val == node.next.next.val: | ||||||||||||||||||||||||||||||||
node.next = node.next.next | ||||||||||||||||||||||||||||||||
node.next = node.next.next | ||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||
node = node.next | ||||||||||||||||||||||||||||||||
Comment on lines
+71
to
+77
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. early return (この場合は early continue と言うのでしょうか) にすると条件判定部が少し読みやすくなると思いました。
Suggested change
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return dummy.next |
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.
そういう考えいいですね。条件文が長すぎるので何かないかなとずっと考えていたのでそのような案を提示してくださるのはあありがたいです。
操作としては特に何も変化はないですが条件を condition = node.next and node.next.next and node.next.val == node.next.next.val というふうにまとめるのも一つかなとは思ったんですけど、これについて可読性の観点から考えをお聞きしてもよろしいでしょうか?
Uh oh!
There was an error while loading. Please reload this page.
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.next = node.next.next
をする時に、重複ノードが消える前のこと(ifの条件)を頭に入れておかないといけないので、認知不可が高いんじゃないでしょうか?例えば、手作業でこの重複削除の作業をすることを考えると、重複を1つにまとめる作業と最後に残った一つを消す作業の二段階に分かれるのではないかと思っていて
その場合って、重複ノードをバーっと消していって、その後で「じゃあ最後に残った一つも消すか」ってやると思うんですよ
「最初の段階でこのノードは重複してて、今最後の一個まで消したから、最後の一個も消そう」とは考えないはずで
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.
この方法だと綺麗にまとめられますね!私が解いた時もできていなかったので勉強になりました。