Slow and Fast pointer are very useful in Linked List.

Concept

Problems

1. Leetcode 876. Middle of the Linked List

class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        ListNode* slow = head;
        ListNode* fast = head;
        while(fast != nullptr && fast->next != nullptr) {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }
};

For Full Solution CLICK HERE


Enjoy blogging!