Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions lc82.py
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:

Choose a reason for hiding this comment

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

長い条件が二回連続して書かれているので、まとめるほうが好みです

    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(-1, head)
        node = dummy

        while node:
            is_duplicated = False
            while node.next and node.next.next and node.next.val == node.next.next.val:
                    is_duplicated = True
                    node.next = node.next.next
            if is_duplicated:
                node.next = node.next.next
            else:
                node = node.next

        return dummy.next

Copy link
Owner Author

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 というふうにまとめるのも一つかなとは思ったんですけど、これについて可読性の観点から考えをお聞きしてもよろしいでしょうか?

Copy link

@Yoshiki-Iwasa Yoshiki-Iwasa Sep 5, 2024

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つにまとめる作業と最後に残った一つを消す作業の二段階に分かれるのではないかと思っていて
その場合って、重複ノードをバーっと消していって、その後で「じゃあ最後に残った一つも消すか」ってやると思うんですよ

「最初の段階でこのノードは重複してて、今最後の一個まで消したから、最後の一個も消そう」とは考えないはずで

Copy link

Choose a reason for hiding this comment

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

あ、これ発想になかったですね。

Copy link

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
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

Choose a reason for hiding this comment

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

early return (この場合は early continue と言うのでしょうか) にすると条件判定部が少し読みやすくなると思いました。

Suggested change
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
while node.next and node.next.next:
if node.next.val != node.next.next.val:
node = node.next
continue
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


return dummy.next