1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/cpp/middle-of-the-linked-list.cpp

28 lines
626 B
C++
Raw Normal View History

/**
* 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;
}
};