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

problems: add middle of the linked list

This commit is contained in:
Matej Focko 2022-07-12 19:09:21 +00:00
parent b600d8cc67
commit 671fe591f6

View file

@ -0,0 +1,27 @@
/**
* 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;
}
};