{"id":4451,"date":"2021-08-30T05:03:29","date_gmt":"2021-08-30T05:03:29","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4451"},"modified":"2023-07-27T12:41:04","modified_gmt":"2023-07-27T12:41:04","slug":"insert-a-node-into-the-middle-of-the-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/","title":{"rendered":"Insert a node into the middle of the linked list"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png\" alt=\"\" \/><\/p>\n<p>The algorithm to insert a node in the middle of a linked list will be covered in this article. One of the key ideas in a linked list is insertion, since it involves dealing with links and changing them as necessary. Let\u2019s attempt to come up with the reasoning behind the method that will insert in middle of linked list.So let&#8217;s dive into the article and learn various algorithm to insert a node in the middle of a linked list.<\/p>\n<h2>How to Insert an Element in the Middle of a Linked List<\/h2>\n<p>We are given a singly linked list and the value x in this problem. The center of the list must have a node with the value x.<\/p>\n<p>With the aid of examples, let&#8217;s study programming online and attempt to comprehend the technique for inserting a node in the center of a linked list.<\/p>\n<p>Suppose the given linked list is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-15.png\" alt=\"\" \/><\/p>\n<ul>\n<li>So now, according to the problem statement, we have to insert a node with value x = 4 into the middle of the list such that our resultant <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-fractional-or-n-k-th-node-in-linked-list\/\" title=\"linked list\">linked list<\/a> will look like:<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-7.png\" alt=\"\" \/><\/p>\n<p><strong>Explanation:<\/strong> Here, the current middle of the list is the node with the value 2, so 4 will be inserted just after 2.<\/p>\n<p>If the given linked list is 1 \u2192 3 \u2192 5 \u2192 7 \u2192 9 \u2192 11 and x = 6.<\/p>\n<ul>\n<li>For the above-linked list, the node with the value 5 is in the middle of the linked list so the node with value x = 6 will be inserted just after the node with the value 5 and our resultant linked list will look like: 1 \u2192 3 \u2192 5 \u2192 6 \u2192 7 \u2192 9 \u2192 11<\/li>\n<\/ul>\n<p><strong>Explanation:<\/strong> Here, the current middle of the list is the node with value 5, so 6 will be inserted just after 5.<\/p>\n<p>I hope from the above examples the problem is clear, and now the main question is how we should approach the algorithm to insert an element in the middle of a linked list. What is the first thing that comes to mind?<\/p>\n<ul>\n<li>Finding out the length of the list and then adding the new node just after the (length\/2)<sup>th<\/sup> node if the length is even, else just after the ((length+1)\/2)<sup>th<\/sup> node if the length is odd.<\/li>\n<li>Well, this will be our approach for the algorithm to insert an element in the middle of a linked list. Then afterward, we will see whether we can optimize it or not.<\/li>\n<\/ul>\n<p>Let us have a glance at the approaches and see where we can optimize the first approach.<\/p>\n<h3>Approach for the Algorithm to Insert a Node at the Middle of Linked List (Using the length)<\/h3>\n<p>In this approach:<\/p>\n<ul>\n<li>We will first find the length of the linked list i.e., the number of nodes in the linked list. <\/li>\n<li>As we know if the length is even, then the middle node is (length\/2)<sup>th<\/sup> node, else the middle is ((length+1)\/2)<sup>th<\/sup> node.<\/li>\n<li>So, if the length is even, we will add the new node after (length\/2)<sup>th<\/sup> node of the linked list, else we will add the new node after ((length+1)\/2)<sup>th<\/sup> node of the linked list.<\/li>\n<\/ul>\n<h3>Algorithm to Insert a Node at the Middle of Linked List<\/h3>\n<ul>\n<li>With the help of list traversal, find the length of the linked list. Let the length be <strong>len<\/strong>.<\/li>\n<li>Now, create a variable <strong>k<\/strong>. <strong>k<\/strong> will <strong>(len\/2)<\/strong> if <strong>len<\/strong> is even, else it will <strong>(len+1)\/2<\/strong>.<\/li>\n<li>Here, <strong>k<\/strong> denotes the middle point of the list.<\/li>\n<li>Traverse <strong>k<\/strong> nodes and add the new node just after the <strong>k<\/strong><sup>th<\/sup> node.<\/li>\n<li>In this way, the new node gets added into the middle of the list.<\/li>\n<\/ul>\n<p>This approach is okay, but can we further optimize it?<\/p>\n<ul>\n<li>If we think carefully, the answer is yes. We can optimize the algorithm to insert an element in the middle of a linked list, as in this technique, we are doing two traversals of the given list.<\/li>\n<\/ul>\n<p>What if we start by taking two pointers, say slow and fast, and make both points to the head initially.<\/p>\n<ul>\n<li>What will happen if we make the slow jump one place and the fast jump two places?<\/li>\n<li>If we notice carefully, by doing the above step when the fast will reach the end of the list, the slow will be pointing to the middle of the list.<\/li>\n<li>With the help of this technique, we can reach the middle node of a linked list in a single pass.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> The reason why slow will be pointing to the middle of the linked list when fast reaches the end is that, as our slow pointer is travelling with half of the speed of the fast pointer, so when fast pointer will reach the end of the linked list, till that time slow pointer will have travelled only half the distance as travelled by fast pointer and hence it will be at the middle of the linked list.<\/p>\n<p>Do you know what the above-explained method is called?<\/p>\n<ul>\n<li>This method is called the tortoise and hare method or the slow and fast pointer method.<\/li>\n<\/ul>\n<p>The slow and fast pointer method has been explained in detail in the approach section and the dry run.<\/p>\n<h3>Approach to insert node at middle of linked list (Slow and Fast Pointer)<\/h3>\n<p>Let us see what the <strong>slow<\/strong> and <strong>fast<\/strong> pointer technique is:<\/p>\n<ul>\n<li>Initially, both the pointers, <strong>slow<\/strong> and <strong>fast<\/strong>, will point to the head of the linked list.<\/li>\n<li>Then, we will start moving both of the pointers. The <strong>fast<\/strong> pointer will jump two places, whereas the <strong>slow<\/strong> pointer will jump only one place. <\/li>\n<li>When the <strong>fast<\/strong> pointer reaches the end of the linked list, the <strong>slow<\/strong> pointer will be pointing to the middle of the linked list.<\/li>\n<li>Now, as we have the <strong>slow<\/strong> pointer pointing to the middle of the list, we will add the new node just after the <strong>slow<\/strong> pointer.<\/li>\n<\/ul>\n<h3>Algorithm to Insert Node at Middle of Linked List<\/h3>\n<ul>\n<li>Create two-pointer <strong>slow<\/strong> and <strong>fast<\/strong>. Both <strong>slow<\/strong> and <strong>fast<\/strong> will be pointing to the head of the list.<\/li>\n<li>Now, the <strong>slow<\/strong> will jump one place and the <strong>fast<\/strong> will jump two places.<\/li>\n<li>When the <strong>fast<\/strong> pointer reaches the end of the list, the <strong>slow<\/strong> will be pointing to the middle of the list.<\/li>\n<li>Add the new node <strong>newNode<\/strong> just after the slow pointer following the below steps.\n<ul>\n<li>newNode &#8211; &gt; next = slow &#8211; &gt; next<\/li>\n<li>slow &#8211; &gt; next = newNode<\/li>\n<\/ul>\n<\/li>\n<li>Finally, our <strong>newNode<\/strong> will be inserted into the middle of the linked list and our objective will be achieved.<\/li>\n<\/ul>\n<h3>DRY RUN of the Algorithm to Insert a Node at the Middle of Linked List<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Insert-a-node-into-the-middle-of-the-linked-list-1.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Insert-a-node-into-the-middle-of-the-linked-list-2.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation of the Algorithm to Insert a Node at the Middle of Linked List<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4453 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_4453 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_4453 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4453 .wpsm_nav-tabs > li.active > a, #tab_container_4453 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4453 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_4453 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_4453 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4453 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4453 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4453 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4453 .wpsm_nav-tabs > li > a:hover , #tab_container_4453 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_4453 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_4453 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_4453 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_4453 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_4453 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_4453 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_4453 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4453 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_4453 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4453 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_4453 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_4453\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4453\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4453_1\" aria-controls=\"tabs_desc_4453_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4453_2\" aria-controls=\"tabs_desc_4453_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4453_3\" aria-controls=\"tabs_desc_4453_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4453_4\" aria-controls=\"tabs_desc_4453_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_4453\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_4453_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/\/ function to create and return a node\r\nstruct Node* getNode(int data)\r\n{\r\n    \/\/ allocating space\r\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\r\n \r\n    \/\/ inserting the required data\r\n    newNode-&gt;data = data;\r\n    newNode-&gt;next = NULL;\r\n    return newNode;\r\n}\r\n \r\n\/\/ function to insert node at the middle\r\n\/\/ of the linked list\r\nvoid insertAtMid(struct Node** head_ref, int x)\r\n{\r\n    \/\/ if list is empty\r\n    if (*head_ref == NULL)\r\n        *head_ref = getNode(x);\r\n    else {\r\n \r\n        \/\/ get a new node\r\n        struct Node* newNode = getNode(x);\r\n \r\n        struct Node* ptr = *head_ref;\r\n        int len = 0;\r\n \r\n        \/\/ calculate length of the linked list\r\n        \/\/, i.e, the number of nodes\r\n        while (ptr != NULL) {\r\n            len++;\r\n            ptr = ptr-&gt;next;\r\n        }\r\n \r\n        \/\/ 'count' the number of nodes after which\r\n        \/\/  the new node is to be inserted\r\n        int count = ((len % 2) == 0) ? (len \/ 2) :\r\n                                    (len + 1) \/ 2;\r\n        ptr = *head_ref;\r\n \r\n        \/\/ 'ptr' points to the node after which\r\n        \/\/ the new node is to be inserted\r\n        while (count-- &gt; 1)\r\n            ptr = ptr-&gt;next;\r\n \r\n        \/\/ insert the 'newNode' and adjust the\r\n        \/\/ required links\r\n        newNode-&gt;next = ptr-&gt;next;\r\n        ptr-&gt;next = newNode;\r\n    }\r\n}\r\n \r\n\/\/ function to display the linked list\r\nvoid display(struct Node* head)\r\n{\r\n    while (head != NULL) {\r\n        printf(&quot;%d &quot;,head-&gt;data);\r\n        head = head-&gt;next;\r\n    }\r\n}\r\n \r\n\/\/ Driver program to test above\r\nint main()\r\n{\r\n    \/\/ Creating the list 1-&gt;2-&gt;4-&gt;5\r\n    struct Node* head = NULL;\r\n    head = getNode(1);\r\n    head-&gt;next = getNode(2);\r\n    head-&gt;next-&gt;next = getNode(4);\r\n    head-&gt;next-&gt;next-&gt;next = getNode(5);\r\n \r\n    printf(&quot;Linked list before insertion: &quot;);\r\n    display(head);\r\n \r\n    int x = 3;\r\n    insertAtMid(&amp;head, x);\r\n \r\n    printf(&quot;&#92;nLinked list after insertion: &quot;);\r\n    display(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4453_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits stdc++.h=&quot;&quot;&gt;\r\n\r\nusing namespace std;\r\n\r\nstruct Node {\r\n    int data;\r\n    Node* next;\r\n};\r\n\r\nNode* getNode(int data)\r\n{\r\n\r\n    Node* newNode = (Node*)malloc(sizeof(Node));\r\n\r\n    newNode-&gt;data = data;\r\n    newNode-&gt;next = NULL;\r\n    return newNode;\r\n}\r\n\r\nvoid insertAtMid(Node** head_ref, int x)\r\n{\r\n\r\n    if (*head_ref == NULL)\r\n        *head_ref = getNode(x);\r\n\r\n    else {\r\n\r\n        Node* newNode = getNode(x);\r\n\r\n        Node* slow = *head_ref;\r\n        Node* fast = *head_ref;\r\n\r\n        while (fast &amp;&amp; fast-&gt;next) {\r\n\r\n            slow = slow-&gt;next;\r\n\r\n            fast = fast-&gt;next-&gt;next;\r\n        }\r\n\r\n        newNode-&gt;next = slow-&gt;next;\r\n        slow-&gt;next = newNode;\r\n    }\r\n}\r\n\r\nvoid display(Node* head)\r\n{\r\n    while (head != NULL) {\r\n        cout &lt;&lt; head-&gt;data &lt;&lt; &quot; &quot;;\r\n        head = head-&gt;next;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    Node* head = NULL;\r\n    head = getNode(1);\r\n    head-&gt;next = getNode(5);\r\n    head-&gt;next-&gt;next = getNode(2);\r\n    head-&gt;next-&gt;next-&gt;next = getNode(7);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = getNode(10);\r\n\r\n    cout &lt;&lt; &quot;Linked list before insertion: &quot;;\r\n    display(head);\r\n\r\n    int x = 4;\r\n    insertAtMid(&amp;head, x);\r\n\r\n    cout &lt;&lt; &quot;&#92;nLinked list after insertion: &quot;;\r\n    display(head);\r\n\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4453_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\nimport java.lang.*;\r\nimport java.io.*;\r\n\r\npublic class LinkedList\r\n{\r\n    static Node head; \r\n\r\n    static class Node {\r\n        int data;\r\n        Node next;\r\n\r\n        Node(int d) {\r\n            data = d;\r\n            next = null;\r\n        }\r\n    }\r\n\r\n    static void insertAtMid(int x)\r\n    {\r\n\r\n        if (head == null)\r\n        head = new Node(x);\r\n\r\n        else {\r\n            Node newNode = new Node(x);\r\n\r\n            Node slow = head;\r\n            Node fast = head;\r\n\r\n            while (fast != null &amp;&amp; fast.next\r\n                                != null)\r\n            {\r\n\r\n                slow = slow.next;\r\n\r\n                fast = fast.next.next;\r\n            }\r\n\r\n            newNode.next = slow.next;\r\n            slow.next = newNode;\r\n        }\r\n    }\r\n\r\n    static void display()\r\n    {\r\n        Node temp = head;\r\n        while (temp != null)\r\n        {\r\n            System.out.print(temp.data + &quot; &quot;);\r\n            temp = temp.next;\r\n        }\r\n    }\r\n\r\n    public static void main (String[] args)\r\n    {\r\n        head = null;\r\n        head = new Node(1);\r\n        head.next = new Node(5);\r\n        head.next.next = new Node(2);\r\n        head.next.next.next = new Node(7);\r\n        head.next.next.next.next = new Node(10);\r\n        \r\n        System.out.println(&quot;Linked list before insertion: &quot;);\r\n        display();\r\n\r\n        int x = 4;\r\n        insertAtMid(x);\r\n\r\n        System.out.println(&quot;&#92;nLinked list after insertion: &quot;);\r\n        display();\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4453_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\r\n    def __init__(self, data):\r\n        self.data = data\r\n        self.next = None\r\n\r\ndef insertAtMid(head, x):\r\n\r\n    if(head == None):\r\n        head = Node(x)\r\n    else:\r\n        \r\n        newNode = Node(x)\r\n\r\n        ptr = head\r\n        length = 0\r\n        \r\n        while(ptr != None):\r\n            ptr = ptr.next\r\n            length += 1\r\n\r\n        if(length % 2 == 0):\r\n            count = length \/ 2\r\n        else:\r\n            count = (length + 1) \/ 2\r\n\r\n        ptr = head\r\n\r\n        while(count &gt; 1):\r\n            count -= 1\r\n            ptr = ptr.next\r\n\r\n        newNode.next = ptr.next\r\n        ptr.next = newNode\r\n\r\ndef display(head):\r\n    temp = head\r\n    while(temp != None):\r\n        print(str(temp.data), end = &quot; &quot;)\r\n        temp = temp.next\r\n\r\n\r\nhead = Node(1)\r\nhead.next = Node(5)\r\nhead.next.next = Node(2)\r\nhead.next.next.next = Node(7)\r\nhead.next.next.next.next = Node(10)\r\n\r\nprint(&quot;Linked list before insertion: &quot;, end = &quot;&quot;)\r\ndisplay(head)\r\n\r\nx = 4\r\ninsertAtMid(head, x)\r\n\r\nprint(&quot;&#92;nLinked list after insertion: &quot; , end = &quot;&quot;)\r\ndisplay(head)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4453 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_4453 a\"),jQuery(\"#tab-content_4453\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>Linked list before insertion: 1 5 2 7 10\nLinked list after insertion: 1 5 2 4 7 10<\/code><\/pre>\n<p><strong>Space Complexity:<\/strong> O(1), as only temporary variables are being created.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nFinding the middle node and adjusting the pointers to incorporate the new node are the two most important steps in inserting a node into the middle of a linked list. We can insert a node into the middle of the linked list without disrupting the existing structure by correctly manipulating the pointers. This operation is commonly used in a variety of applications where an ordered or sorted linked list must be maintained.<\/p>\n<h2>Frequently Asked Questions (FAQs)<\/h2>\n<p><strong>Q1: What is a linked list?<\/strong><br \/>\n<strong>Ans.<\/strong> A linked list is a linear data structure where elements are stored in nodes. Each node contains a data element and a pointer that points to the next node in the list. It allows for dynamic memory allocation and efficient insertion and deletion operations.<\/p>\n<p><strong>Q2: How do I insert a node into the middle of a linked list?<\/strong><br \/>\n<strong>Ans.<\/strong> To insert a node into the middle of a linked list, follow these steps:<\/p>\n<ul>\n<li>Find the middle node using the slow and fast pointer technique.<\/li>\n<li>Create a new node with the desired data.<\/li>\n<li>Adjust the pointers of the previous node and the new node to incorporate the new node.<\/li>\n<li>Update the pointers of the new node and the next node to maintain the list&#8217;s connectivity.<\/li>\n<\/ul>\n<p><strong>Q3: What is the time complexity of inserting a node into the middle of a linked list?<\/strong><br \/>\n<strong>Ans.<\/strong> The time complexity of inserting a node into the middle of a linked list is O(n), where n is the number of nodes in the list. This is because finding the middle node requires traversing the list, which takes linear time. Once the middle node is found, the actual insertion operation takes constant time.<\/p>\n<p><strong>Q4: Can I insert a node into the middle of a singly linked list?<\/strong><br \/>\n<strong>Ans.<\/strong> Yes, you can insert a node into the middle of a singly linked list. The process involves finding the middle node using the slow and fast pointer technique and adjusting the pointers accordingly.<\/p>\n<p><strong>Q5: What happens if the linked list has an even number of nodes?<\/strong><br \/>\n<strong>Ans.<\/strong> If the linked list has an even number of nodes, there are two possible middle nodes. In such cases, you can choose to insert the new node either before or after the exact middle node, depending on your requirements.<\/p>\n<p><strong>Q6: What if the linked list is empty?<\/strong><br \/>\n<strong>Ans.<\/strong> If the linked list is empty, inserting a node into the middle is not possible since there are no nodes, to begin with. In that case, you can consider inserting the node at the head or simply creating a new linked list with the single node.<\/p>\n<p><strong>Q7: Can I insert a node at the beginning or end of a linked list using the same method?<\/strong><br \/>\n<strong>Ans.<\/strong> No, inserting a node at the beginning or end of a linked list requires different steps compared to inserting a node in the middle. For insertion at the beginning, you need to adjust the head pointer. For insertion at the end, you need to traverse the list until you reach the last node and then adjust the pointers accordingly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The algorithm to insert a node in the middle of a linked list will be covered in this article. One of the key ideas in a linked list is insertion, since it involves dealing with links and changing them as necessary. Let\u2019s attempt to come up with the reasoning behind the method that will insert [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-4451","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Insert a node into the middle of the linked list | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to insert a node into the middle of the linked list. This blog explains the most efficient approach to insert a node into the middle of the linked list.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Insert a node into the middle of the linked list | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to insert a node into the middle of the linked list. This blog explains the most efficient approach to insert a node into the middle of the linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-30T05:03:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-27T12:41:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Insert a node into the middle of the linked list\",\"datePublished\":\"2021-08-30T05:03:29+00:00\",\"dateModified\":\"2023-07-27T12:41:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/\"},\"wordCount\":1630,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/\",\"name\":\"Insert a node into the middle of the linked list | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png\",\"datePublished\":\"2021-08-30T05:03:29+00:00\",\"dateModified\":\"2023-07-27T12:41:04+00:00\",\"description\":\"Learn the most efficient way to insert a node into the middle of the linked list. This blog explains the most efficient approach to insert a node into the middle of the linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Insert a node into the middle of the linked list\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Insert a node into the middle of the linked list | Linked List | Prepbytes","description":"Learn the most efficient way to insert a node into the middle of the linked list. This blog explains the most efficient approach to insert a node into the middle of the linked list.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Insert a node into the middle of the linked list | Linked List | Prepbytes","og_description":"Learn the most efficient way to insert a node into the middle of the linked list. This blog explains the most efficient approach to insert a node into the middle of the linked list.","og_url":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-30T05:03:29+00:00","article_modified_time":"2023-07-27T12:41:04+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Insert a node into the middle of the linked list","datePublished":"2021-08-30T05:03:29+00:00","dateModified":"2023-07-27T12:41:04+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/"},"wordCount":1630,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/","name":"Insert a node into the middle of the linked list | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png","datePublished":"2021-08-30T05:03:29+00:00","dateModified":"2023-07-27T12:41:04+00:00","description":"Learn the most efficient way to insert a node into the middle of the linked list. This blog explains the most efficient approach to insert a node into the middle of the linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644911211669-Insert%20a%20node%20into%20the%20middle-04.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-into-the-middle-of-the-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Insert a node into the middle of the linked list"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=4451"}],"version-history":[{"count":11,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4451\/revisions"}],"predecessor-version":[{"id":17387,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4451\/revisions\/17387"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}