🔥✅✅ Beats 100% with Proof | Very Easy to Understand ✅✅🔥
Proof - Upvote if you are watching the proof\n\n\n> Solved on Mar 07, 2024 06:44 GMT +5:30\n\n# Intuition\n Describe your first thoughts on how to solve this pr
# Proof - Upvote if you are watching the proof\n![image.png](https://assets.leetcode.com/users/images/9af201b4-1e45-4a15-a6e8-21c994031fe1_1709774783.9641798.png)\n\n> **Solved on Mar 07, 2024 06:44 GMT +5:30**\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this solution is to use two pointers, one slow and one fast. The slow pointer moves one step at a time, while the fast pointer moves two steps at a time. By the time the fast pointer reaches the end of the linked list, the slow pointer will be at the middle node.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Check for base cases: If the `head` is null or there is only one node in the list, return the `head` as the middle node.\n2. Initialize two pointers, `slow` and `fast`, both initially pointing to the `head` of the linked list.\n3. Use a while loop to iterate through the linked list. In each iteration, move the `slow` pointer one step and the `fast` pointer two steps.\n4. When the `fast` pointer reaches the end of the list (i.e., `fast` or `fast.next` is null), the `slow` pointer will be at the middle node.\n5. Return the `slow` pointer as the result.\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n> where n is the number of nodes in the linked list. The slow pointer traverses the list once.\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n> as we are using only two pointers regardless of the size of the linked list.\n\n# Code\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 */\nclass Solution {\n    public ListNode middleNode(ListNode head) {\n        if(head == null || head.next == null) return head;\n        ListNode fast = head;\n        ListNode slow = head;\n        while(fast != null && fast.next != null){\n            slow = slow.next;\n            fast = fast.next.next;\n        }\n        return slow;\n    }\n}\n```\n```C []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     struct ListNode *next;\n * };\n */\nstruct ListNode* middleNode(struct ListNode* head) {\n    if(head == NULL || head->next == NULL) return head;\n    struct ListNode* fast = head;\n    struct ListNode* slow = head;\n    while(fast != NULL && fast->next != NULL){\n        slow = slow->next;\n        fast = fast->next->next;\n    }\n    return slow;\n}\n\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* middleNode(ListNode* head) {\n        if(head == nullptr || head->next == nullptr) return head;\n        ListNode* fast = head;\n        ListNode* slow = head;\n        while(fast != nullptr && fast->next != nullptr){\n            slow = slow->next;\n            fast = fast->next->next;\n        }\n        return slow;\n    }\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        if(head == null || head.next == null) return head;\n        ListNode fast = head;\n        ListNode slow = head;\n        while(fast != null && fast.next != null){\n            slow = slow.next;\n            fast = fast.next.next;\n        }\n        return slow;\n    }\n}\n\n```\n```Python []\n# Definition for singly-linked list.\n# class ListNode(object):\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution(object):\n    def middleNode(self, head):\n        """\n        :type head: ListNode\n        :rtype: ListNode\n        """\n        if not head or not head.next:\n            return head\n        fast = head\n        slow = head\n        while fast and fast.next:\n            slow = slow.next\n            fast = fast.next.next\n        return slow\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: Optional[ListNode]) -> Optional[ListNode]:\n        if not head or not head.next:\n            return head\n        fast = head\n        slow = head\n        while fast and fast.next:\n            slow = slow.next\n            fast = fast.next.next\n        return slow\n\n```\n```Javascript []\n/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n *     this.val = (val === undefined ? 0 : val);\n *     this.next = (next === undefined ? null : next);\n * }\n */\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar middleNode = function (head) {\n    if (!head || !head.next) {\n        return head;\n    }\n    let fast = head;\n    let slow = head;\n    while (fast && fast.next) {\n        slow = slow.next;\n        fast = fast.next.next;\n    }\n    return slow;\n};\n\n```\n```Typescript []\n/**\n * Definition for singly-linked list.\n * class ListNode {\n *     val: number;\n *     next: ListNode | null;\n *     constructor(val?: number, next?: ListNode | null) {\n *         this.val = (val === undefined ? 0 : val);\n *         this.next = (next === undefined ? null : next);\n *     }\n * }\n */\n\nfunction middleNode(head: ListNode | null): ListNode | null {\n    if (!head || !head.next) {\n        return head;\n    }\n    let fast = head;\n    let slow = head;\n    while (fast && fast.next) {\n        slow = slow.next;\n        fast = fast.next.next;\n    }\n    return slow;\n}\n\n```\n\n# Upvote - Please\n![image.png](https://assets.leetcode.com/users/images/c4dde627-82c0-4598-9720-9e75b70d375c_1709774864.5916247.png)\n
['Linked List', 'Two Pointers', 'C', 'Python', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript', 'C#']