Skip to content

82. Remove Duplicates from Sorted List II#6

Open
TORUS0818 wants to merge 4 commits intomainfrom
82
Open

82. Remove Duplicates from Sorted List II#6
TORUS0818 wants to merge 4 commits intomainfrom
82

Conversation

@TORUS0818
Copy link
Copy Markdown
Owner

Comment thread medium/82/answer.md
prev = dummy
current = head
while current:
while current.next and current.val == current.next.val:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、とりあえず、while 突入して、あとから何が起きたかを考えるの、素直じゃない気がします。後でどうせ分岐するならば確認してから突入したほうが分かりやすくないですか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。

        while curr_node:
            if not curr_node.next or curr_node.val != curr_node.next.val:
                prev_node = prev_node.next
                curr_node = curr_node.next
                continue

こんな感じで、今のノードと次のノードの値が異なる場合の判定を先に済ませる感じでしょうか。

同じノードを飛ばす処理の方に意識がいってしまって、このような構成になってしまったんだと思います。。

Copy link
Copy Markdown
Owner Author

@TORUS0818 TORUS0818 May 5, 2024

Choose a reason for hiding this comment

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

同じような話で、

        while curr_node and curr_node.next:
            if curr_node.val != curr_node.next.val:
                prev_node = prev_node.next
                curr_node = curr_node.next
                continue

こっちの方が良いですかね。

Comment thread medium/82/answer.md
duplicated_value = current.val
continue

previous.next = ListNode(current.val)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここで新たなノードを作っていますが、previous.next = currentにするとLinkedListの繋ぎ変え操作だけで行けると思います。
ただ、whileの抜けた部分でもprevious.next = currentする必要がありますが。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。
個人的にループの外で後始末したくないなという気持ちがありますが、オブジェクトを毎回生成するのも無駄ですよね。

previous.next = currentがないと、最後のノードの値が連続したようなケース(1 -> 2 -> 2 -> 2とか)でコケる感じですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

確かに私も抜けた後にprevious.next = currentするのはできればしたくないと思います。
ただ、新しいノードを作る方法だと空間計算量がO(N)になるのがデメリットかなと思います。

Comment thread medium/82/answer.md
Comment on lines +102 to +103
previous = dummy
current = head
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

previous_nodenodeでもよいかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます!
文字数も減りますし、こちらの方が良いですね。

Comment thread medium/82/answer.md
Comment on lines +139 to +146
if head.val == duplicated_val:
head = delete_duplicates_helper(head.next, head.val)
elif head.next and head.val == head.next.val:
head = delete_duplicates_helper(head.next, head.val)
else:
head.next = delete_duplicates_helper(head.next, None)

return head
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

early returnしちゃって

            if head.val == duplicated_val:
                return delete_duplicates_helper(head.next, head.val)
            if head.next and head.val == head.next.val:
                return delete_duplicates_helper(head.next, head.val)
            head.next = delete_duplicates_helper(head.next, None)
            return head

するのはいかがでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。
確かにこちらの方が条件ごとにシンプルに分かれていて読みやすい気がします。

今までearly returnが選択肢の候補に入ることがあまりなかったので、今後は意識してみようと思います。

TORUS0818 added 2 commits May 7, 2024 11:03
変数名を修正したバージョンを追加(Step4)
再帰の実装をearly-return方式にアップデート
Comment thread medium/82/answer.md
prev_node = prev_node.next
else:
prev_node.next = curr_node.next
curr_node = curr_node.next
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

このStep 3は読みにくいと感じました。重複値がある場合とない場合の2つを意識して読み進める必要がありますよね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

有難うございます。

#6 (comment)
こちらのご指摘も同様ですよね。

先に重複値がない場合を処理してしまって、後半で重複のある場合の処理をすることで、問題をシンプルにしていくイメージでしょうか。

新たにノードオブジェクトを生成しない形式で書き直し
@TORUS0818
Copy link
Copy Markdown
Owner Author

odaさん、sakupan102さん、liquo-riceさん、レビューを有難うございました!

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.

4 participants