✅Best approach explained with GFG image
If you are confuse with even and odd nodes you can dry run on images given below\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \
If you are confuse with even and odd nodes you can dry run on images given below\n![Screenshot from 2024-03-07 06-23-26.png](https://assets.leetcode.com/users/images/c5ccf9df-7e1f-4fdb-a8ae-45985746c1b7_1709772822.67654.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Just imagine you are climbing stairs with your friend\n2. you both start together from bottom of stairs\n3. you are taking one step at a time and your friend is taking two step at time\n4. when he reach to top of stairs where will you be\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialization: Start with two pointers, fast and slow, both pointing to the head of the list.\n\n2. Traversal: Move the fast pointer two steps at a time and the slow pointer one step at a time. This ensures that when the fast pointer reaches the end of the list, the slow pointer will be at the middle node.\n\n3. Find the Middle Node: After traversal, the slow pointer will be at the middle node of the list.\n\n4. Edge Case Handling: Check if the list is empty or contains only one node. In such cases, the middle node is the head itself.\n\n5. Return: Return the node pointed to by the slow pointer as the middle node.\n\nExample for odd nodes\n![Screenshot from 2024-03-07 12-14-44.png](https://assets.leetcode.com/users/images/bbdeca94-ff7b-47ee-87bc-4e84f3368bc1_1709793900.2784297.png)\n\nif you want to check for even nodes imagine 5th node is NULL. dry run where will be slow pointer reach  fast pointer reach to 5th node\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```c++ []\nclass Solution {\npublic:\n    ListNode* middleNode(ListNode* head) {\n       if(head == NULL || head->next == NULL)return head;\n        ListNode* fast=head->next;\n        ListNode* slow=head;\n\n        while(fast != NULL){\n            fast = fast->next;\n            if(fast != NULL)fast = fast->next;\n\n            slow = slow->next;\n        }\n\n        return slow;\n    }\n};\n```\n```java []\nclass Solution {\n    public ListNode middleNode(ListNode head) {\n        if (head == null || head.next == null)\n            return head;\n\n        ListNode fast = head;\n        ListNode slow = 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```\n```c []\nListNode* middleNode(ListNode* head) {\n        \n        ListNode* slow = head;\n        ListNode* fast =  head;\n\t\t\n        while(fast && fast->next){   \n            slow = slow->next;\n            fast = fast->next->next;\n        }\n        return slow;\n    }\n```\n```python []\nclass ListNode(object):\n    def __init__(self, val=0, next=None):\n        self.val = val\n        self.next = next\n\nclass Solution(object):\n    def middleNode(self, head):\n        if head is None or head.next is None:\n            return head\n\n        fast = head\n        slow = head\n\n        while fast is not None:\n            fast = fast.next\n            if fast is not None:\n                fast = fast.next\n                slow = slow.next\n\n        return slow\n```\n``` python3 []\nclass Solution:\n    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:\n        start=head\n        end=head\n        while start and start.next:\n            start,end=start.next.next,end.next\n        return end\n```\n```javascript []\nfunction ListNode(val, next) {\n    this.val = (val === undefined ? 0 : val);\n    this.next = (next === undefined ? null : next);\n}\n\nvar middleNode = function(head) {\n    if (head === null || head.next === null)\n        return head;\n\n    let fast = head;\n    let slow = 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```c# []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     public int val;\n *     public ListNode next;\n *     public ListNode(int val=0, ListNode next=null) {\n *         this.val = val;\n *         this.next = next;\n *     }\n * }\n */\npublic class Solution {\n    public ListNode MiddleNode(ListNode head) {\n        ListNode[] nodes = new ListNode[100];\n        int index = 0;\n\n        while (head != null) {\n            nodes[index++] = head;\n            head = head.next;\n        }\n\n        return nodes[index / 2];\n    }\n}\n```\n```kotlin []\nfun middleNode(head: ListNode?): ListNode? {\n        var p1 = head\n        var p2 = head\n        while(p2 != null && p2.next != null) {\n            p1 = p1?.next\n            p2 = p2?.next?.next\n        }\n        return p1\n    }\n```\n\nHello guys can i use images of any website like this will it create any problem\nif it is not allowed please leave a comment \nexplaning with images make it easy
['Linked List', 'Two Pointers', 'C', 'Python', 'C++', 'Java', 'Python3', 'Kotlin', 'JavaScript', 'C#']