iterative and recursive slow fast
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nThe given C++ cod
![Screenshot from 2024-03-07 05-45-30.png](https://assets.leetcode.com/users/images/a20edee0-7f75-47f4-9303-b937638bd0b6_1709770556.221646.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe given C++ code defines a class Solution that contains a method middleNode for finding the middle node of a singly-linked list. The linked list is represented using the ListNode structure, where each node has an integer value (val) and a pointer to the next node (next).\n\nHere\'s an explanation of the approach used in the middleNode method:\n\n1-  Two pointers, slow and fast, are initialized to the head of the linked list.\n  \n2- A while loop is used to traverse the list with the fast pointer moving two steps at a time and the slow pointer moving one step at a time.\n  \n3- The loop continues until the fast pointer reaches the end of the list (i.e., becomes nullptr).\n  \n4- Inside the loop, both fast and slow pointers are moved accordingly.\n  \n5-  The slow pointer will end up pointing to the middle node when the fast pointer reaches the end because it moves at half the speed of the fast pointer.\n\n6- Finally, the slow pointer is returned, which now points to the middle node of the linked list.\n\n# Complexity\n- Time complexity:0(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code ITERATIVE\n``` c++ []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* middleNode(ListNode* head) {\n        ListNode *slow=head;\n        ListNode *fast=head;\n\n        while(fast){\n            fast=fast->next;\n            if(fast){\n            fast=fast->next;\n            slow=slow->next;\n        }\n        }\n        return slow;\n    }\n};\n```\n``` java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\n\npublic class Solution {\n    public ListNode middleNode(ListNode head) {\n        ListNode slow = head;\n        ListNode fast = head;\n\n        while (fast != null) {\n            fast = fast.next;\n            if (fast != null) {\n                fast = fast.next;\n                slow = slow.next;\n            }\n        }\n\n        return slow;\n    }\n}\n```\n``` python []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def middleNode(self, head: ListNode) -> ListNode:\n        slow = head\n        fast = head\n\n        while fast:\n            fast = fast.next\n            if fast:\n                fast = fast.next\n                slow = slow.next\n\n        return slow\n```\n\n# code 2 RECURSIVE\n\n``` C++ []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* findMiddle(ListNode* slow, ListNode* fast) {\n        if (!fast || !fast->next) {\n            return slow;\n        }\n\n        return findMiddle(slow->next, fast->next->next);\n    }\n    ListNode* middleNode(ListNode* head) {\n        if (!head || !head->next) {\n            return head;\n        }\n\n        return findMiddle(head, head);\n    }\n};\n\n```\n``` JAVA []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\npublic class Solution {\n     private ListNode findMiddle(ListNode slow, ListNode fast) {\n        if (fast == null || fast.next == null) {\n            return slow;\n        }\n\n        return findMiddle(slow.next, fast.next.next);\n    }\n    public ListNode middleNode(ListNode head) {\n        if (head == null || head.next == null) {\n            return head;\n        }\n\n        return findMiddle(head, head);\n    }\n\n   \n}\n```\n``` PYTHON3 []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def middleNode(self, head: ListNode) -> ListNode:\n        if not head or not head.next:\n            return head\n\n        return self.findMiddle(head, head)\n\n    def findMiddle(self, slow, fast):\n        if not fast or not fast.next:\n            return slow\n\n        return self.findMiddle(slow.next, fast.next.next)\n\n```
['Linked List', 'Two Pointers', 'C++', 'Java', 'Python3']