mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
27 lines
626 B
C++
27 lines
626 B
C++
/**
|
|
* 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) {
|
|
auto slow = head;
|
|
auto fast = head ? head->next : nullptr;
|
|
|
|
while (fast != nullptr) {
|
|
slow = slow->next;
|
|
fast = fast->next;
|
|
if (fast) {
|
|
fast = fast->next;
|
|
}
|
|
}
|
|
|
|
return slow;
|
|
}
|
|
};
|