Skip to content

Conversation

@shining-ai
Copy link
Owner

for i, node in enumerate(lists):
if not node:
continue
heapq.heappush(min_heap, (node.val, i, node))
Copy link

Choose a reason for hiding this comment

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

インデックスをヒープに入れるのは、問題文上不要ですか?

Choose a reason for hiding this comment

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

これがないと、定義されていないListNodeの比較になって問題が出るのですかね。

Copy link
Owner Author

@shining-ai shining-ai May 24, 2024

Choose a reason for hiding this comment

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

問題文上は不要です。

これがないと、定義されていないListNodeの比較になって問題が出るのですかね。

おっしゃる通りで、同値の比較のために入れていました。
この旨はコメントで書いておかないと、疑問を持たれてしまいますね。

Copy link

Choose a reason for hiding this comment

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

同じ値があったときに、オブジェクトの < が定義されていないからエラーということですね。

for i, node in enumerate(lists):
if not node:
continue
heapq.heappush(min_heap, (node.val, i, node))

Choose a reason for hiding this comment

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

これがないと、定義されていないListNodeの比較になって問題が出るのですかね。

node = sentinel
while min_heap:
val, index, smallest_head = heapq.heappop(min_heap)
node.next = ListNode(val)

Choose a reason for hiding this comment

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

新しいノードを作らず、繋ぎ直しでもできますか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

こういうことですかね。

val, index, smallest_head = heapq.heappop(min_heap)
node.next = smallest_head
node = node.next

既存のノードのnextは必ず上書きされるので、新しく作った方が分かりやすいかなと考えてました。

Choose a reason for hiding this comment

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

in-placeなのか新しく作るのかは、要件によるかと思います。

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