100% Beats Easy Approach ✅✅ | | Middle of the Linked List | | c++ | | Java| | Kotlin  | | Javascript
\n\nWe can solve this problem itritaive \n\n# Intuition\nThe problem requires finding the middle node of a given singly-linked list. One approach to solving thi
![image.png](https://assets.leetcode.com/users/images/0d1e5b9f-7a63-406a-a2d4-814bc837e4e2_1709770279.6892052.png)\n\nWe can solve this problem itritaive \n\n# Intuition\nThe problem requires finding the middle node of a given singly-linked list. One approach to solving this problem is to iterate through the list to determine its length. Then, we can traverse halfway through the list to find the middle node.\n\n# Approach\n1. Create a function to calculate the length of the linked list.\n2. Calculate the length of the list.\n3. Traverse halfway through the list to find the middle node.\n4. Return the middle node.\n\n# Complexity\n- Time complexity: \n  - Calculating the length of the linked list takes O(n) time.\n  - Traversing halfway through the list also takes O(n/2) time.\n  - Overall time complexity is O(n).\n\n- Space complexity:\n  - The space complexity is O(1) because we are not using any extra space proportional to the size of the input.\n\n# Code\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    int listLength(ListNode* head) {\n        int len = 0;\n        while (head != NULL) {\n            len++;\n            head = head->next;\n        }\n        return len;\n    }\n\n    ListNode* middleNode(ListNode* head) {\n        int len = listLength(head);\n        int middle = len / 2;\n        ListNode* temp = head;\n        int count = 0;\n        while (count < middle) {\n            temp = temp->next;\n            count++;\n        }\n        return temp;\n    }\n};\n```\n```java []\n/**\n * Definition for singly-linked list.\n * 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 int listLength(ListNode head) {\n        int len = 0;\n        while (head != null) {\n            len++;\n            head = head.next;\n        }\n        return len;\n    }\n\n    public ListNode middleNode(ListNode head) {\n        int len = listLength(head);\n        int middle = len / 2;\n        ListNode temp = head;\n        int count = 0;\n        while (count < middle) {\n            temp = temp.next;\n            count++;\n        }\n        return temp;\n    }\n}\n\n```\n```Kotlin []\n/**\n * Definition for singly-linked list.\n * class ListNode(var `val`: Int) {\n *     var next: ListNode? = null\n * }\n */\n\nclass Solution {\n    fun listLength(head: ListNode?): Int {\n        var length = 0\n        var current = head\n        while (current != null) {\n            length++\n            current = current.next\n        }\n        return length\n    }\n\n    fun middleNode(head: ListNode?): ListNode? {\n        val length = listLength(head)\n        val middle = length / 2\n        var temp = head\n        var count = 0\n        while (count < middle) {\n            temp = temp?.next\n            count++\n        }\n        return temp\n    }\n}\n\n```\n```javascript []\n/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n *     this.val = val;\n *     this.next = null;\n * }\n */\n\n/**\n * @param {ListNode} head\n * @return {number}\n */\nvar listLength = function(head) {\n    let length = 0;\n    let current = head;\n    while (current !== null) {\n        length++;\n        current = current.next;\n    }\n    return length;\n};\n\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar middleNode = function(head) {\n    const length = listLength(head);\n    const middle = Math.floor(length / 2);\n    let temp = head;\n    let count = 0;\n    while (count < middle) {\n        temp = temp.next;\n        count++;\n    }\n    return temp;\n};\n\n```\n\n\n\n# Dry Run\nLet\'s dry run the code with the input `[1, 2, 3, 4, 5]`:\n\n1. Initialize `len = 0`, `temp = head`, and `count = 0`.\n2. Iterate through the list to find its length. After this step, `len = 5`.\n3. Calculate the middle as `middle = 5 / 2 = 2`.\n4. Traverse halfway through the list. After traversal, `temp` points to the node with value `3`.\n5. Return `temp`, which is the middle node.
['Linked List', 'C++', 'Java', 'Kotlin', 'JavaScript']