Simple solution || Beginner friendly || Detailed Explanation || Gits✅✅👈👈
Guys if the explanation and the solution is helpful then up vote me please!!!\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\n\n# Intuition \uD83D\uDC48\n Desc
# Guys if the explanation and the solution is helpful then up vote me please!!!\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\n\n# Intuition \uD83D\uDC48\n<!-- Describe your first thoughts on how to solve this problem. -->\nBrute Force Solution:\nIn the Brute Force Solution:\n\n- Initially, we determine the size of the LinkedList by iterating through it, incurring a time complexity of O(n), where n is the number of nodes.\n`while (current != null) {\n    count++;\n    current = current.next;\n}\n`\n- A condition is introduced to check if the list is empty (if(temp.next == null)), returning head.next if true.\n- We then calculate the middle element\'s position.\n- The second for loop iterates through the linked list until it reaches the middle node.\n- It moves current by count / 2 steps. This works because count represents the total number of nodes, and count / 2 gives the index of the middle node.\n- If the list has an even number of nodes, this loop will reach the second middle node.\n`for (int i = 0; i < count / 2; i++) {\n    current = current.next;\n}\n`\n- The total time complexity is O(n) + O(n), with O(1) space complexity.\n```\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        int count = 0;\n        ListNode current = head;\n\n        // First iteration to count the number of nodes\n        while (current != null) {\n            count++;\n            current = current.next;\n        }\n\n        // Reset current pointer for the second iteration\n        current = head;\n\n        // Second iteration to find the second middle node\n        for (int i = 0; i < count / 2; i++) {\n            current = current.next;\n        }\n\n        return current;\n    }\n}\n```\n# Approach \uD83D\uDC48\n<!-- Describe your approach to solving the problem. -->\nTo optimize this question, we employ the two-pointer approach, reducing time complexity to O(n):\n- Initialize two pointers, first_ptr and second_ptr, both pointing to the head.\n- Iterate through the list, advancing first_ptr by two steps and second_ptr by one step.\n- When first_ptr reaches the tail, second_ptr will be at the middle of the LinkedList.\n# Complexity \uD83D\uDC48\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe implementation involves a single loop, resulting in a time complexity of O(n).\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nSince no additional space is utilized, the space complexity is a  highly efficient O(1).\n# Code \uD83D\uDC48\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    struct ListNode* first_ptr = head;\n    struct ListNode* second_ptr = head;\n    while(first_ptr != NULL && first_ptr -> next != NULL){\n        second_ptr = second_ptr -> next;\n        first_ptr = first_ptr -> next -> next;\n    }\n    return second_ptr;\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        ListNode* first_ptr = head;\n        ListNode* second_ptr = head;\n        while(first_ptr != NULL && first_ptr -> next != NULL){\n            second_ptr = second_ptr -> next;\n            first_ptr = first_ptr -> next -> next;\n        }\n        return second_ptr;\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 */\nclass Solution {\n    public ListNode middleNode(ListNode head) {\n        ListNode First_ptr = head , second_ptr = head;\n        while(First_ptr != null && First_ptr.next != null){\n            second_ptr = second_ptr.next;\n            First_ptr = First_ptr.next.next;\n        }\n        return second_ptr ;\n    }\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    let first_ptr = head\n    let second_ptr = head\n    while(first_ptr && first_ptr.next){\n        second_ptr = second_ptr.next\n        first_ptr = first_ptr.next.next\n    }\n    return second_ptr\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 First_ptr = head , second_ptr = head;\n        while(First_ptr != null && First_ptr.next != null){\n            second_ptr = second_ptr.next;\n            First_ptr = First_ptr.next.next;\n        }\n        return second_ptr ;\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        first_ptr = head\n        second_ptr = head\n        while first_ptr and first_ptr.next:\n            second_ptr = second_ptr.next\n            first_ptr = first_ptr.next.next\n        return second_ptr\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        first_ptr = head\n        second_ptr = head\n        while first_ptr and first_ptr.next:\n            second_ptr = second_ptr.next\n            first_ptr = first_ptr.next.next\n        return second_ptr\n```\n# If you like explanation and code then please send a like.\n![Screenshot 2023-12-05 225836.png](https://assets.leetcode.com/users/images/fd4956df-a612-48d5-b6f5-57822559cfec_1701797328.693149.png)\n
['Linked List', 'Two Pointers', 'C', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript', 'C#']