1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-20 01:56:57 +02:00
LeetCode/cpp/middle-of-the-linked-list.cpp
Matej Focko b229608723
cpp(chore): add clang-format style and format
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-01-03 12:06:54 +01:00

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