Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 876_middle_of_the_linked_list/memo.md
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 のとき何を返すのかとか、関数名どうするかとか考慮すべき要素が増える気はする。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

抽象化するならば GetNth も用意したほうがいいのでは?

あと、速度を実測しての比較もよければ。2 pass のほうが少し遅いですかね。でもそんなに変わらないので、production ならこっちとりますかね。

* two "middle"s のとき first を返してください、みたいなのにも対応しやすいとは思う。

### step3

* step1 のほうがよいかなという気がする。`fast`, `slow` の変数名は好みがあるかもしれない。
24 changes: 24 additions & 0 deletions 876_middle_of_the_linked_list/step1.cpp
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;
}
};
36 changes: 36 additions & 0 deletions 876_middle_of_the_linked_list/step2.cpp
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;
Copy link
Copy Markdown

@nodchip nodchip Nov 16, 2025

Choose a reason for hiding this comment

The 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;
}
};
22 changes: 22 additions & 0 deletions 876_middle_of_the_linked_list/step3.cpp
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;
}
};