-
Notifications
You must be signed in to change notification settings - Fork 0
876. Middle of the Linked List #22
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
ryosuketc
wants to merge
1
commit into
main
Choose a base branch
from
876_middle_of_the_linked_list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # 876. Middle of the Linked List | ||
|
|
||
| https://leetcode.com/problems/middle-of-the-linked-list/ | ||
|
|
||
| ## Comments | ||
|
|
||
| ### step1 | ||
|
|
||
| * slow, fast を使うか、あるいは 2 pass でノードの総数を求めてから 2nd pass で返すというのもできるだろう | ||
| * slow, fast が 1 pass なのと、シミュレーションする限りシンプルに書けそうだったのでそうした。 | ||
| * ただ production なら後者の方法で書くかもしれない。読み手にとってはより直感的だと思う | ||
| * 問題の例に与えられている、node が偶数、奇数のときでそれぞれシミュレーションした | ||
|
|
||
| ```cpp | ||
| // 1,1 -> 2,3 -> 3,5 (fast->next == nullptr) | ||
| // 1,1 -> 2,3 -> 3,5 -> 4,nullptr (fast == nullptr) | ||
| ``` | ||
|
|
||
| * あとは edge case として node 数が 0, 1, 2 のあたりも脳内でテストした。 | ||
| * 7:00 くらいで AC | ||
|
|
||
| ### step2 | ||
|
|
||
| * 2 pass も一応書いた。思ったより冗長になったけど、一応こっちのほうがわかりやすいかなという気はする。 | ||
| * size (length) を求める関数だったので、例に倣って 1-index にしたが 0-index でもよかったかもしれない。ただその場合 head == nullptr のとき何を返すのかとか、関数名どうするかとか考慮すべき要素が増える気はする。 | ||
| * two "middle"s のとき first を返してください、みたいなのにも対応しやすいとは思う。 | ||
|
|
||
| ### step3 | ||
|
|
||
| * step1 のほうがよいかなという気がする。`fast`, `slow` の変数名は好みがあるかもしれない。 | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode() : val(0), next(nullptr) {} | ||
| * ListNode(int x) : val(x), next(nullptr) {} | ||
| * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
| * }; | ||
| */ | ||
| // 1,1 -> 2,3 -> 3,5 (fast->next == nullptr) | ||
| // 1,1 -> 2,3 -> 3,5 -> 4,nullptr (fast == nullptr) | ||
| class Solution { | ||
| public: | ||
| ListNode* middleNode(ListNode* head) { | ||
| ListNode* slow = head; | ||
| ListNode* fast = head; | ||
| while (fast && fast->next) { | ||
| slow = slow->next; | ||
| fast = fast->next->next; | ||
| } | ||
| return slow; | ||
| } | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode() : val(0), next(nullptr) {} | ||
| * ListNode(int x) : val(x), next(nullptr) {} | ||
| * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
| * }; | ||
| */ | ||
| // 1,1 -> 2,3 -> 3,5 (fast->next == nullptr) | ||
| // 1,1 -> 2,3 -> 3,5 -> 4,nullptr (fast == nullptr) | ||
| class Solution { | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自分なら for 文で書くと思います。 int middle_index = GetListSize(head) / 2;
ListNode* node = head;
for (int index = 0; index < middle_index; ++index) {
node = node->next;
}
return node;自分にはこちらのほうがシンプル見感じられます。好みの問題かもしれません。 |
||
| int index = 1; | ||
| ListNode* node = head; | ||
| while (index != middle_index) { | ||
| ++index; | ||
| node = node->next; | ||
| } | ||
| return node; | ||
| } | ||
| private: | ||
| int GetListSize(ListNode* head) { | ||
| int size = 0; | ||
| ListNode* node = head; | ||
| while (node) { | ||
| ++size; | ||
| node = node->next; | ||
| } | ||
| return size; | ||
| } | ||
| }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * struct ListNode { | ||
| * int val; | ||
| * ListNode *next; | ||
| * ListNode() : val(0), next(nullptr) {} | ||
| * ListNode(int x) : val(x), next(nullptr) {} | ||
| * ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
| * }; | ||
| */ | ||
| class Solution { | ||
| public: | ||
| ListNode* middleNode(ListNode* head) { | ||
| ListNode* slow = head; | ||
| ListNode* fast = head; | ||
| while (fast && fast->next) { | ||
| slow = slow->next; | ||
| fast = fast->next->next; | ||
| } | ||
| return slow; | ||
| } | ||
| }; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
抽象化するならば GetNth も用意したほうがいいのでは?
あと、速度を実測しての比較もよければ。2 pass のほうが少し遅いですかね。でもそんなに変わらないので、production ならこっちとりますかね。