mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +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;
|
|
}
|
|
};
|