Open
Conversation
oda
reviewed
Nov 15, 2025
| ### step2 | ||
|
|
||
| * 2 pass も一応書いた。思ったより冗長になったけど、一応こっちのほうがわかりやすいかなという気はする。 | ||
| * size (length) を求める関数だったので、例に倣って 1-index にしたが 0-index でもよかったかもしれない。ただその場合 head == nullptr のとき何を返すのかとか、関数名どうするかとか考慮すべき要素が増える気はする。 |
There was a problem hiding this comment.
抽象化するならば GetNth も用意したほうがいいのでは?
あと、速度を実測しての比較もよければ。2 pass のほうが少し遅いですかね。でもそんなに変わらないので、production ならこっちとりますかね。
nodchip
reviewed
Nov 16, 2025
| public: | ||
| ListNode* middleNode(ListNode* head) { | ||
| // Return the second node when there are two "middle"s. 1-indexed. | ||
| int middle_index = GetListSize(head) / 2 + 1; |
There was a problem hiding this comment.
自分なら for 文で書くと思います。
int middle_index = GetListSize(head) / 2;
ListNode* node = head;
for (int index = 0; index < middle_index; ++index) {
node = node->next;
}
return node;自分にはこちらのほうがシンプル見感じられます。好みの問題かもしれません。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
876. Middle of the Linked List
https://leetcode.com/problems/middle-of-the-linked-list/