{"id":6880,"date":"2025-06-17T18:39:00","date_gmt":"2025-06-17T13:09:00","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6880"},"modified":"2025-06-17T18:39:00","modified_gmt":"2025-06-17T13:09:00","slug":"find-the-middle-node-of-a-singly-linked-list","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/find-the-middle-node-of-a-singly-linked-list\/","title":{"rendered":"Find the middle node of a singly linked list"},"content":{"rendered":"\n<!-- PrismJS for Syntax Highlighting -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\" rel=\"stylesheet\">\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: #E58C32;\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  border-radius: 12px;\n  background-color: #f9fafb;\n}\n\n.wp_blog_explanation h2 {\n  font-size: 1.4rem;\n  color: #E58C32;\n  margin-bottom: 0.75rem;\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n  color: #1f2937;\n}\n\n.wp_blog_explanation code {\n  padding: 3px 6px;\n  border-radius: 4px;\n  background: #e5e7eb;\n  font-family: 'Courier New', monospace;\n}\n\n.wp_blog_explanation img {\n  width: 100%;\n  border-radius: 12px;\n  margin-top: 1rem;\n}\n\n\/* Tabs *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid #E58C32;\n  color: #E58C32;\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  background-color: white;\n  transition: background 0.3s ease, color 0.3s ease;\n}\n\n.wp_blog_code-tab-button.active {\n  background: #E58C32;\n  color: white;\n}\n\n\/* Code Panel *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: #111827;\n  border-radius: 12px;\n}\n\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  color: #f3f4f6;\n  background: #111827;\n  border-radius: 12px;\n}\n<\/style>\n\n<div class=\"wp_blog_explanation\">\n  <h2>Problem Statement:<\/h2>\n  <p>Find the middle node of a singly linked list using the slow and fast pointer approach.<\/p>\n\n  <h2>Approach:<\/h2>\n  <ul>\n    <li>Use two pointers: <code>slow<\/code> and <code>fast<\/code>.<\/li>\n    <li>Initialize both at the head of the list.<\/li>\n    <li>Move <code>slow<\/code> one step and <code>fast<\/code> two steps at a time.<\/li>\n    <li>When <code>fast<\/code> reaches the end, <code>slow<\/code> will be at the middle.<\/li>\n  <\/ul>\n\n  <h2>Examples:<\/h2>\n  <ul>\n    <li><strong>Input:<\/strong> <code>[1,2,3,4,5]<\/code><\/li>\n    <li><strong>Output:<\/strong> <code>[3,4,5]<\/code><\/li>\n    <li><strong>Explanation:<\/strong> The middle node is node <code>3<\/code>.<\/li>\n  <\/ul>\n\n  <ul>\n    <li><strong>Input:<\/strong> <code>[1,2,3,4,5,6]<\/code><\/li>\n    <li><strong>Output:<\/strong> <code>[4,5,6]<\/code><\/li>\n    <li><strong>Explanation:<\/strong> There are two middle nodes: <code>3<\/code> and <code>4<\/code>. We return the second one.<\/li>\n  <\/ul>\n\n  <h2>Constraints:<\/h2>\n  <ul>\n    <li>The number of nodes in the list is in the range <code>[1, 100]<\/code>.<\/li>\n    <li><code>1 <= Node.val <= 100<\/code><\/li>\n  <\/ul>\n\n\n  <h2>Time & Space Complexity:<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> <code>O(n)<\/code><\/li>\n    <li><strong>Space Complexity:<\/strong> <code>O(1)<\/code><\/li>\n  <\/ul>\n\n  <h2>Use Case:<\/h2>\n  <p>This technique is efficient with O(n) time and O(1) space, commonly used in problems like finding the start of a loop, checking palindromes, etc.<\/p>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cs\">C#<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\/**\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 slow = head;\n    let fast = head;\n    while (fast && fast.next) {\n        slow = slow.next;\n        fast = fast.next.next;\n    }\n    return slow;\n};<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\"># 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: Optional[ListNode]\n        :rtype: Optional[ListNode]\n        \"\"\"\n        slow = head\n        fast = head\n        while fast and fast.next:\n            slow = slow.next\n            fast = fast.next.next\n        return slow<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\/**\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 slow = head;\n        ListNode fast = head;\n        while (fast != null && fast.next != null) {\n            slow = slow.next;\n            fast = fast.next.next;\n        }\n        return slow;\n    }\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\/**\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* slow = head;\n        ListNode* fast = head;\n        while (fast && fast->next) {\n            slow = slow->next;\n            fast = fast->next->next;\n        }\n        return slow;\n    }\n};<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\/**\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* slow = head;\n    struct ListNode* fast = head;\n    while (fast != NULL && fast->next != NULL) {\n        slow = slow->next;\n        fast = fast->next->next;\n    }\n    return slow;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n    <pre><code class=\"language-csharp\">\/**\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 slow = head;\n        ListNode fast = head;\n        while (fast != null && fast.next != null) {\n            slow = slow.next;\n            fast = fast.next.next;\n        }\n        return slow;\n    }\n}<\/code><\/pre>\n  <\/div>\n<\/div>\n\n<script>\ndocument.addEventListener('DOMContentLoaded', () => {\n  const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n  const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\n  buttons.forEach(button => {\n    button.addEventListener('click', () => {\n      const lang = button.getAttribute('data-lang');\n      buttons.forEach(btn => btn.classList.remove('active'));\n      contents.forEach(content => content.classList.remove('active'));\n      button.classList.add('active');\n      document.querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`).classList.add('active');\n    });\n  });\n});\n<\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: Find the middle node of a singly linked list using the slow and fast pointer approach. Approach: Use two pointers: slow and fast. Initialize both at the head of the list. Move slow one step and fast two steps at a time. When fast reaches the end, slow will be at the middle.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,176,175,811,810,174,172,173],"tags":[],"class_list":["post-6880","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-csharp","category-cplusplus","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6880"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6880\/revisions"}],"predecessor-version":[{"id":6881,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6880\/revisions\/6881"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}