Easy Solution Beats 100% with Java || Python || C++ || C# 🔥😊
\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal of this code is to find and return the middle node of a linked list. I
![Screenshot 2024-03-07 091852.png](https://assets.leetcode.com/users/images/13db4f11-64da-4114-9919-a76d61a1698e_1709783708.4773962.png)\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal of this code is to find and return the middle node of a linked list. If the linked list has an odd number of nodes, it returns the middle node, and if it has an even number of nodes, it returns the second middle node.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The function begins by checking if the given linked list is empty or has only one node. In such cases, the single node (or null) is considered the middle and is returned.\n\n2. The code then initializes two pointers: `head` and `middle`. The `head` pointer is used to iterate through the linked list, while the `middle` pointer lags behind, effectively tracking the `middle` node.\n\n3. A counter variable `(count)` is used to keep track of the number of nodes visited.\n\n4. The while loop continues until the end of the linked list is reached (i.e., `head.next` becomes null). In each iteration, the `head` pointer advances to the next node, and the `count` is incremented.\n\n5. If the count is even `(count % 2 == 0)`, the `middle` pointer is moved to the next node. This ensures that the `middle` pointer lags behind by half the number of nodes, making it point to the middle node when the loop exits.\n\n6. Finally, the `middle` pointer points to the middle node, and it is returned as the result.\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```java []\nclass Solution {\n    public ListNode middleNode(ListNode head) {\n        if(head==null || head.next==null){\n            return head;\n        }\n        ListNode middle = head;\n        int count  = 1;\n        while(head.next != null){\n            head = head.next;\n            count++;\n            if(count%2==0){\n                middle = middle.next;\n            }\n        }\n        return middle;\n    }\n}\n```\n```python []\nclass Solution:\n    def middleNode(self, head: ListNode) -> ListNode:\n        if not head or not head.next:\n            return head\n        \n        middle = head\n        count = 1\n        \n        while head.next:\n            head = head.next\n            count += 1\n            if count % 2 == 0:\n                middle = middle.next\n        \n        return middle\n```\n```C++ []\nclass Solution {\npublic:\n    ListNode* middleNode(ListNode* head) {\n        if (!head || !head->next) {\n            return head;\n        }\n\n        ListNode* middle = head;\n        int count = 1;\n\n        while (head->next) {\n            head = head->next;\n            count++;\n            if (count % 2 == 0) {\n                middle = middle->next;\n            }\n        }\n\n        return middle;\n    }\n};\n```\n```C# []\npublic class Solution {\n    public ListNode MiddleNode(ListNode head) {\n        if (head == null || head.next == null) {\n            return head;\n        }\n\n        ListNode middle = head;\n        int count = 1;\n\n        while (head.next != null) {\n            head = head.next;\n            count++;\n            if (count % 2 == 0) {\n                middle = middle.next;\n            }\n        }\n\n        return middle;\n    }\n}\n```\n\n![Angry.jpg](https://assets.leetcode.com/users/images/a9ed6292-a8cf-4801-b5da-9072b6884508_1709783934.6760895.jpeg)\n
['Python', 'C++', 'Java', 'Python3', 'C#']