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

Conversation

rinost081
Copy link
Owner

@rinost081 rinost081 commented Sep 5, 2024

  1. Remove Duplicates from Sorted List を解きました。
    問題: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
    言語: Python
    レビューをお願いします。

@rinost081 rinost081 changed the title Leet Code 83 Leet Code 82 Sep 5, 2024
lc82.py Outdated
dummy = ListNode(-1, head)
node = dummy

if node:

Choose a reason for hiding this comment

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

これwhileじゃないですか??

Copy link
Owner Author

Choose a reason for hiding this comment

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

そうですね、間違えた箇所をコピペしてました、、


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

この方法だと綺麗にまとめられますね!私が解いた時もできていなかったので勉強になりました。

@Yoshiki-Iwasa
Copy link

みました! 再帰的に解く方法もあるので、ちょろっと見てみるといいかもです

@h1rosaka
Copy link

h1rosaka commented Sep 8, 2024

確認しました。私のプルリクを見てくださっていたようなので、ご存知かとは思いますが、tailを使った書き方もできます。たくさんnextを書かずにスッキリ書くことができて、個人的にはこちらが好みです。

Comment on lines +71 to +77
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

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

@rinost081 rinost081 changed the title Leet Code 82 82. Remove Duplicates from Sorted List Dec 5, 2024
@rinost081 rinost081 changed the title 82. Remove Duplicates from Sorted List 82. Remove Duplicates from Sorted List II Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants