{"id":4217,"date":"2021-08-24T02:02:04","date_gmt":"2021-08-24T02:02:04","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4217"},"modified":"2023-07-27T12:23:25","modified_gmt":"2023-07-27T12:23:25","slug":"delete-middle-of-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/","title":{"rendered":"Delete Middle Node of Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png\" alt=\"\" \/><\/p>\n<p>Linked lists consist of interconnected nodes, with each node containing data and a pointer to the next node in the sequence. Removing the middle node of a linked list poses a significant challenge.  Here, we will discuss how to delete middle of linked list in an efficient way. <\/p>\n<h2>How to Delete the Middle Node of a Linked List?<\/h2>\n<p>The example given below will help in understanding the concept to delete middle element of linked list.<\/p>\n<p><strong>Input:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-11.png\" alt=\"\" \/><\/p>\n<p>Since we have to delete middle element of linked list such that the resultant linked list after deletion will be like as shown in the output below.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-5.png\" alt=\"\" \/><\/p>\n<p><strong>Explanation:<\/strong><br \/>\nNow, in the above given linked list, 3rd node is the middle node of the linked list. Since we have to delete middle element of linked list, we remove the node containing 10 as the data. After deleting the node, we get the final linked list as given below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-2-1.png\" alt=\"\" \/><\/p>\n<p>Here we have just made a connection between node(5) and node(15).<\/p>\n<p><strong>Things to observe about the deletion of nodes in a linked list<\/strong><\/p>\n<ul>\n<li>If we take some more examples, we will notice that we need access to the previous and the next node of the node that we wish to delete. So, that we can connect the previous node to the next node<\/li>\n<\/ul>\n<p>Say, if the deleted node is the target, its previous node is prev and its next node is next1. So, to do the deletion of the target node from the linked list, we need to do the following operations:<\/p>\n<ul>\n<li>prev\u2192next = next1<\/li>\n<li>And finally free the target node<\/li>\n<\/ul>\n<p>Similarly, in this problem, we need to first find the position of the middle node and node previous to the middle node and then following the above-mentioned steps of deletion we have to delete the middle element of the linked list.<\/p>\n<p>So now we will see how to get the middle node and which node will be the middle node of the linked list in case of even length and odd length linked list.<\/p>\n<p><strong>Observations for the position of the middle element of linked list<\/strong><\/p>\n<p><strong>Even Length Linked List:<\/strong><\/p>\n<ul>\n<li>If there are an even number of nodes in the linked list, there will be two middle nodes. From these two middle nodes, we need to delete the second middle node of the linked list.<\/li>\n<li>For example, if the given linked list is 5\u219210\u219215\u219220\u219225\u219230, then 15 and 20 will be the two middle nodes of the linked list, and we will remove the second middle node which is 20.<\/li>\n<li>So our final modified linked list after deletion will look like this 5\u219210\u219215\u219225\u219230.<\/li>\n<\/ul>\n<p><strong>Odd Length Linked List:<\/strong><\/p>\n<p>If there are an odd number of nodes in the linked list, there will be a single middle node, and we will delete that node from the linked list.<br \/>\nFor example, if the given linked list is 5\u219210\u219215\u219220\u219225, then 15 will be the middle node of the linked list, and we will remove this node from the linked list.<br \/>\nSo our final modified linked list after deletion will look like this 5\u219210\u219220\u219230.<\/p>\n<p><strong>Note:<\/strong> If there is just one node present in the input linked list, that node should be removed and a new head should be returned.<\/p>\n<h3>Naive Approach to Delete the Middle Node of a Linked List<\/h3>\n<p>The naive approach to deleting the middle node is to count the number of nodes N in a linked list first, then delete the (N\/2)th node using the simple deletion method.<\/p>\n<p>The (N\/2)th node is the middle node of the linked list.<\/p>\n<p><strong>Algorithm of Naive Approach to Delete the Middle Node of a Linked List<\/strong><br \/>\nThe algorithm for the above-mentioned Approach is given below.<\/p>\n<ul>\n<li>\n<p>If the head is NULL, return NULL.<\/p>\n<\/li>\n<li>\n<p>If the next of head is NULL, delete the head and return NULL.<\/p>\n<\/li>\n<li>\n<p>Create a node Head_copy and make it point to the head.<\/p>\n<\/li>\n<li>\n<p>Initialize a counter variable count = 0, and now with help of head, traverse the linked list incrementing the count variable by 1 for each node of the linked list.<\/p>\n<\/li>\n<li>\n<p>Finally, when the traversal of the list will be over count will contain the count of the number of nodes in the linked list.<\/p>\n<\/li>\n<li>\n<p>The position of the middle node of the linked list will be given by (count\/2), store it in a variable mid.<\/p>\n<\/li>\n<li>\n<p>Now starting from the head of the linked list, traverse until the below condition holds:<\/p>\n<pre><code>while (mid-- > 1) {\n\nhead = head\u2192next;\n\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>At the end of the while loop, you will be at the node previous of the middle node. So now make head\u2192next = head\u2192next\u2192next to delete the middle node of the linked list.<\/p>\n<\/li>\n<li>\n<p>Finally, return Head_copy, which is the head of the newly formed list.<\/p>\n<\/li>\n<\/ul>\n<p><strong>Code Implementation to Delete Middle Element of Linked List (By Using the Naive Approach)<\/strong><br \/>\nThe code to delete middle element in linked list in C, C++, Java, and Python is given below.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4218 {\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_4218 .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_4218 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4218 .wpsm_nav-tabs > li.active > a, #tab_container_4218 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4218 .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_4218 .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_4218 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4218 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4218 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4218 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4218 .wpsm_nav-tabs > li > a:hover , #tab_container_4218 .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_4218 .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_4218 .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_4218 .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_4218 .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_4218 .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_4218 .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_4218 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4218 .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_4218 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4218 .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_4218 .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_4218\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4218\">\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_4218_1\" aria-controls=\"tabs_desc_4218_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_4218_2\" aria-controls=\"tabs_desc_4218_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_4218_3\" aria-controls=\"tabs_desc_4218_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_4218_4\" aria-controls=\"tabs_desc_4218_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_4218\">\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_4218_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\nstruct Node {\r\n    int val;\r\n    struct Node* next;\r\n};\r\n\/\/ counting the number of nodes present in the list\r\nint count_nodes(struct Node* head)\r\n{\r\n    int n_count = 0;\r\n    while (head != NULL) {\r\n        head = head-&gt;next;\r\n        n_count++;\r\n    }\r\n    return n_count;\r\n}\r\n\/\/ returns head of the newly formed list\r\n\/\/ after deleting the middle element.\r\nstruct Node* delete_middle(struct Node* head)\r\n{\r\n    if (head == NULL)\r\n        return NULL;\r\n    if (head-&gt;next == NULL) {\r\n        free(head);\r\n        return NULL;\r\n    }\r\n    struct Node* Head_copy = head;\r\n    \/\/ total nodes currently there in the list\r\n    int count = count_nodes(head);\r\n    \/\/ position of middle element in the list\r\n    int mid = count \/ 2;\r\n    \/\/ Delete the middle node\r\n    while (mid-- &gt; 1) {\r\n        head = head-&gt;next;\r\n    }\r\n    \/\/ Delete the middle node\r\n    head-&gt;next = head-&gt;next-&gt;next;\r\n    return Head_copy;\r\n}\r\n\/\/ Function to display the list\r\nvoid display_List(struct Node* x)\r\n{\r\n    while (x != NULL) {\r\n        printf(&quot;%d -&gt;&quot;, x-&gt;val);\r\n        x = x-&gt;next;\r\n    }\r\n    \/\/ Last element points to null\r\n    printf(&quot;NULL&#92;n&quot;);\r\n}\r\n\/\/ function to create a new node.\r\nstruct Node* newNode(int value)\r\n{\r\n    struct Node* t = (struct Node*)malloc(sizeof(struct Node));\r\n    t-&gt;val = value;\r\n    t-&gt;next = NULL;\r\n    return t;\r\n}\r\n\/\/ Driver Function\r\nint main()\r\n{\r\n    \/\/ Adding elements to the empty list\r\n    struct Node* head = newNode(5);\r\n    head-&gt;next = newNode(10);\r\n    head-&gt;next-&gt;next = newNode(15);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(20);\r\n     head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(25);\r\n    printf(&quot;Original List&#92;n&quot;);\r\n    display_List(head);\r\n    head = delete_middle(head);\r\n    printf(&quot;List after deleting the middle element&#92;n&quot;);\r\n    display_List(head);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_4218_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\/\/ Program to delete middle element of a linked list\r\n#include &lt;bits stdc++.h&gt;\r\nusing namespace std;\r\n\/\/ Node in a linked list\r\nstruct Node {\r\n    int val;\r\n    struct Node* next;\r\n};\r\n\/\/ counting the number of nodes present in the list\r\nint count_nodes(struct Node* head)\r\n{\r\n    int n_count = 0;\r\n    while (head != NULL) {\r\n        head = head-&gt;next;\r\n        n_count++;\r\n    }\r\n    return n_count;\r\n}\r\n\/\/ returns head of the newly formed list\r\n\/\/ after deleting the middle element.\r\nstruct Node* delete_middle(struct Node* head)\r\n{\r\n    if (head == NULL)\r\n        return NULL;\r\n    if (head-&gt;next == NULL) {\r\n        delete head;\r\n        return NULL;\r\n    }\r\n    struct Node* Head_copy = head;\r\n    \/\/ total nodes currently there in the list\r\n    int count = count_nodes(head);\r\n    \/\/ position of middle element in the list\r\n    int mid = count \/ 2;\r\n    \/\/ Delete the middle node\r\n    while (mid-- &gt; 1) {\r\n        head = head-&gt;next;\r\n    }\r\n    \/\/ Delete the middle node\r\n    head-&gt;next = head-&gt;next-&gt;next;\r\n    return Head_copy;\r\n}\r\n\/\/ Function to display the list\r\nvoid display_List(struct Node* x)\r\n{\r\n    while (x != NULL) {\r\n        cout &lt;&lt; x-&gt;val &lt;&lt; &quot;-&gt;&quot;;\r\n        x = x-&gt;next;\r\n    }\r\n    \/\/ Last element points to null\r\n    cout &lt;&lt; &quot;NULL&#92;n&quot;;\r\n}\r\n\/\/ function to create a new node.\r\nNode* newNode(int value)\r\n{\r\n    struct Node* t = new Node;\r\n    t-&gt;val = value;\r\n    t-&gt;next = NULL;\r\n    return t;\r\n}\r\n\/\/ Driver Function\r\nint main()\r\n{\r\n    \/\/ Adding elements to the empty list\r\n    struct Node* head = newNode(5);\r\n    head-&gt;next = newNode(10);\r\n    head-&gt;next-&gt;next = newNode(15);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(20);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(25);\r\n    cout &lt;&lt; &quot;Original List&quot; &lt;&lt; endl;\r\n    display_List(head);\r\n    head = delete_middle(head);\r\n    cout &lt;&lt; &quot;List after deleting the middle element&quot;&lt;&lt; endl;\r\n    display_List(head);\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_4218_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\nclass DeleteMiddle {\r\n\r\n\t\/* Link list Node *\/\r\n\tstatic class Node {\r\n\t\tint data;\r\n\t\tNode next;\r\n\t}\r\n\r\n\t\/\/ Utility function to create a new node.\r\n\tstatic Node newNode(int data)\r\n\t{\r\n\t\tNode temp = new Node();\r\n\t\ttemp.data = data;\r\n\t\ttemp.next = null;\r\n\t\treturn temp;\r\n\t}\r\n\r\n\t\/\/ count of nodes\r\n\tstatic int countOfNodes(Node head)\r\n\t{\r\n\t\tint count = 0;\r\n\t\twhile (head != null) {\r\n\t\t\thead = head.next;\r\n\t\t\tcount++;\r\n\t\t}\r\n\t\treturn count;\r\n\t}\r\n\tstatic Node deleteMid(Node head)\r\n\t{\r\n\t\tif (head == null)\r\n\t\t\treturn null;\r\n\t\tif (head.next == null) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\tNode copyHead = head;\r\n\r\n\t\r\n\t\tint count = countOfNodes(head);\r\n\r\n\t\t\/\/ Find the middle node\r\n\t\tint mid = count \/ 2;\r\n\r\n\t\t\/\/ Delete the middle node\r\n\t\twhile (mid-- > 1) {\r\n\t\t\thead = head.next;\r\n\t\t}\r\n\r\n\t\t\/\/ Delete the middle node\r\n\t\thead.next = head.next.next;\r\n\r\n\t\treturn copyHead;\r\n\t}\r\n\r\n\t\/\/ A utility function to print a given linked list\r\n\tstatic void printList(Node ptr)\r\n\t{\r\n\t\twhile (ptr != null) {\r\n\t\t\tSystem.out.print(ptr.data + \"->\");\r\n\t\t\tptr = ptr.next;\r\n\t\t}\r\n\t\tSystem.out.println(\"NULL\");\r\n\t}\r\n\r\n\t\/* Driver code*\/\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\t\/* Start with the empty list *\/\r\n\t\tNode head = newNode(5);\r\n\t\thead.next = newNode(10);\r\n\t\thead.next.next = newNode(15);\r\n\t\thead.next.next.next = newNode(20);\r\n        head.next.next.next.next = newNode(25);\r\n\r\n\t\tSystem.out.println(\"Given Linked List\");\r\n\t\tprintList(head);\r\n\r\n\t\thead = deleteMid(head);\r\n\r\n\t\tSystem.out.println(\r\n\t\t\t\"Linked List after deletion of middle\");\r\n\t\tprintList(head);\r\n\t}\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_4218_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\n# Node in a linked list\r\nclass Node:\r\n\t\r\n\tdef __init__(self):\r\n\t\t\r\n\t\tself.data = 0\r\n\t\tself.next = None\r\n\t\r\n# counting the number of nodes present in the list\r\ndef countOfNodes(head):\r\n\r\n\tcount = 0\r\n\t\r\n\twhile (head != None):\r\n\t\thead = head.next\r\n\t\tcount += 1\r\n\t\r\n\treturn count\r\n\r\n# returns head of the newly formed list\r\n# after deleting the middle element.\r\ndef deleteMid(head):\r\n\r\n\tif (head == None):\r\n\t\treturn None\r\n\tif (head.next == None):\r\n\t\tdel head\r\n\t\treturn None\r\n\r\n\tcopyHead = head\r\n\r\n\t# after deleting the middle element.\r\n\tcount = countOfNodes(head)\r\n\r\n\t# Find the middle node\r\n\tmid = count \/\/ 2\r\n\r\n\t# Delete the middle node\r\n\twhile (mid > 1):\r\n\t\tmid -= 1\r\n\t\thead = head.next\r\n\r\n\t# Delete the middle node\r\n\thead.next = head.next.next\r\n\r\n\treturn copyHead\r\n\r\n# Function to display the list\r\ndef printList(ptr):\r\n\r\n\twhile (ptr != None):\r\n\t\tprint(ptr.data, end = '->')\r\n\t\tptr = ptr.next\r\n\t\r\n\tprint('NULL')\r\n\t\r\n# function to create a new node.\r\ndef newNode(data):\r\n\r\n\ttemp = Node()\r\n\ttemp.data = data\r\n\ttemp.next = None\r\n\treturn temp\r\n\r\n# Driver Code\r\nif __name__=='__main__':\r\n\t\r\n\t# Start with the empty list\r\n\thead = newNode(5)\r\n\thead.next = newNode(10)\r\n\thead.next.next = newNode(15)\r\n\thead.next.next.next = newNode(20)\r\n\thead.next.next.next.next = newNode(25)\r\n\r\n\tprint(\"Given Linked List\")\r\n\tprintList(head)\r\n\r\n\thead = deleteMid(head)\r\n\r\n\tprint(\"Linked List after deletion of middle\")\r\n\tprintList(head)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_4218 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_4218 a\"),jQuery(\"#tab-content_4218\"));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>Original List\n5\u219210\u219215\u219220\u219225\u2192NULL\nList after deleting the middle element\n5\u219210\u219220\u219225\u2192NULL<\/code><\/pre>\n<p><strong>Time Complexity:<\/strong> O(n), as we are traversing the linked list only twice.<br \/>\n<strong>Space Complexity:<\/strong> O(1), No extra space is needed.<\/p>\n<p>In the above-mentioned naive approach, we are traversing the linked list twice:<\/p>\n<ul>\n<li>First traversal to find the length of the linked list<\/li>\n<li>Then, in the second traversal, moving up to (length\/2)th node of the linked list and delete it.<\/li>\n<\/ul>\n<p>The first question we should ask ourselves is that do we actually need to find the length of the linked list to delete the middle node of the linked list.<\/p>\n<p>The answer is No, we don\u2019t need to find the length of the linked list to delete the middle node of the linked list.<\/p>\n<h3>Optimized Approach to Delete Middle Element of Linked List<\/h3>\n<p>Now in this approach, we will try to find the middle of the linked list in one traversal. We will see how can we tackle this:<\/p>\n<ul>\n<li>What if we start by taking two pointers, say slow and fast, and make both of them point to head initially.<\/li>\n<li>Now what will happen if we make a slow jump in one place and a fast jump in two places (fast moving with twice as speed as slow).<\/li>\n<li>If we notice carefully by doing the above steps, we will see that 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 help of this technique, we can reach the middle of the linked list in one single pass, and hence our objective of reaching the middle of the linked list and deleting it will be achieved in one single traversal of the list. This technique is known as the double-pointer method.<\/li>\n<\/ul>\n<p>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. When the fast pointer will reach the end of the linked list, till that time slow pointer will have travelled only half the distance travelled by the fast pointer and hence it will be in the middle of the linked list.<\/p>\n<p>So in this way, using slow and fast pointers, we can find the position of the middle element of the linked list and then using its previous node we can delete the middle node of the linked list.<\/p>\n<p><strong>Algorithm of Optimized Approach to Delete Middle of Linked List<\/strong><br \/>\nThe algorithm is given below.<\/p>\n<ul>\n<li>\n<p>Create two-pointers, slow and fast.<\/p>\n<\/li>\n<li>\n<p>We will also create a temporary pointer prev which will keep track of the previous node of the slow pointer.<\/p>\n<\/li>\n<li>\n<p>Initially, both slow and fast will be pointing to the head of the linked list.<\/p>\n<\/li>\n<li>\n<p>Now make the slow pointer jump one place and the fast pointer jump two places. The pointer will move further till the fast pointer reaches the end of the linked list.<\/p>\n<\/li>\n<li>\n<p>When the fast pointer reaches the end, the slow pointer will be pointing to the middle of the linked list.<\/p>\n<\/li>\n<li>\n<p>Now using the prev pointer which is keeping track of the previous node to the node pointed by the slow pointer, remove the node pointed by slow from the linked list by performing the below operations:<br \/>\n1) prev\u2192next = slow-&gt;next<br \/>\n2) delete(slow)<\/p>\n<\/li>\n<li>\n<p>Finally, the middle node has been deleted from the linked list and we return the head of the linked list.<\/p>\n<\/li>\n<\/ul>\n<p><strong>Dry Run to Delete Middle of Linked List<\/strong><br \/>\nThe dry run to delete middle element in linked list using the optimized approach is given below for a better understanding of the algorithm.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Delete-middle-of-linked-list.png\" alt=\"\" \/><\/p>\n<p><strong>Code Implementation to Delete Middle Element of Linked List (Using Optimized Approach)<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4220 {\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_4220 .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_4220 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4220 .wpsm_nav-tabs > li.active > a, #tab_container_4220 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4220 .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_4220 .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_4220 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4220 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4220 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4220 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4220 .wpsm_nav-tabs > li > a:hover , #tab_container_4220 .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_4220 .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_4220 .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_4220 .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_4220 .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_4220 .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_4220 .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_4220 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4220 .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_4220 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4220 .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_4220 .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_4220\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4220\">\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_4220_1\" aria-controls=\"tabs_desc_4220_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_4220_2\" aria-controls=\"tabs_desc_4220_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_4220_3\" aria-controls=\"tabs_desc_4220_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_4220_4\" aria-controls=\"tabs_desc_4220_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_4220\">\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_4220_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\nstruct Node {\r\n    int val;\r\n    struct Node* next;\r\n};\r\n\/\/ counting the number of nodes present in the list\r\nint count_nodes(struct Node* head)\r\n{\r\n    int n_count = 0;\r\n    while (head != NULL) {\r\n        head = head-&gt;next;\r\n        n_count++;\r\n    }\r\n    return n_count;\r\n}\r\n\/\/ returns head of the newly formed list\r\n\/\/ after deleting the middle element.\r\nstruct Node* delete_middle(struct Node* head)\r\n{\r\n    if (head == NULL)\r\n        return NULL;\r\n    if (head-&gt;next == NULL) {\r\n        free(head);\r\n        return NULL;\r\n    }\r\n    struct Node* Head_copy = head;\r\n    \/\/ total nodes currently there in the list\r\n    int count = count_nodes(head);\r\n    \/\/ position of middle element in the list\r\n    int mid = count \/ 2;\r\n    \/\/ Delete the middle node\r\n    while (mid-- &gt; 1) {\r\n        head = head-&gt;next;\r\n    }\r\n    \/\/ Delete the middle node\r\n    head-&gt;next = head-&gt;next-&gt;next;\r\n    return Head_copy;\r\n}\r\n\/\/ Function to display the list\r\nvoid display_List(struct Node* x)\r\n{\r\n    while (x != NULL) {\r\n        printf(&quot;%d -&gt;&quot;, x-&gt;val);\r\n        x = x-&gt;next;\r\n    }\r\n    \/\/ Last element points to null\r\n    printf(&quot;NULL&#92;n&quot;);\r\n}\r\n\/\/ function to create a new node.\r\nstruct Node* newNode(int value)\r\n{\r\n    struct Node* t = (struct Node*)malloc(sizeof(struct Node));\r\n    t-&gt;val = value;\r\n    t-&gt;next = NULL;\r\n    return t;\r\n}\r\n\/\/ Driver Function\r\nint main()\r\n{\r\n    \/\/ Adding elements to the empty list\r\n    struct Node* head = newNode(5);\r\n    head-&gt;next = newNode(10);\r\n    head-&gt;next-&gt;next = newNode(15);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(20);\r\n     head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(25);\r\n    printf(&quot;Original List&#92;n&quot;);\r\n    display_List(head);\r\n    head = delete_middle(head);\r\n    printf(&quot;List after deleting the middle element&#92;n&quot;);\r\n    display_List(head);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_4220_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\/\/ Program to delete middle element of a linked list\r\n#include &lt;bits stdc++.h&gt;\r\nusing namespace std;\r\n\/\/ Node in a linked list\r\nstruct Node {\r\n    int val;\r\n    struct Node* next;\r\n};\r\n\/\/ returns head of the newly formed list\r\n\/\/ after deleting the middle element.\r\nstruct Node* delete_middle(struct Node* head)\r\n{\r\n    if (head == NULL)\r\n        return NULL;\r\n    if (head-&gt;next == NULL) {\r\n        delete head;\r\n        return NULL;\r\n    }\r\n\/\/ Inorder to reach the middle element,\r\n\/\/ Initializing slow and fast pointers\r\n    struct Node* slow = head;\r\n    struct Node* fast = head;  \r\n\/\/ Finding out the middle as well as\r\n\/\/ previous of middle.\r\n\/\/ Store previous of slow   \r\nstruct Node* prev;\r\n    while (fast != NULL &amp;&amp; fast-&gt;next != NULL) {\r\n        fast = fast-&gt;next-&gt;next;\r\n        prev = slow;\r\n        slow = slow-&gt;next;\r\n    }\r\n    \/\/ Now, delete the middle element\r\n    prev-&gt;next = slow-&gt;next;\r\n    delete slow;\r\n    return head;\r\n}\r\n\/\/ Function to display the list\r\nvoid display_List(struct Node* x)\r\n{\r\n    while (x != NULL) {\r\n        cout &lt;&lt; x-&gt;val &lt;&lt; &quot;-&gt;&quot;;\r\n        x = x-&gt;next;\r\n    }\r\n    \/\/ Last element points to null\r\n    cout &lt;&lt; &quot;NULL&#92;n&quot;;\r\n}\r\n\/\/ function to create a new node.\r\nNode* newNode(int value)\r\n{\r\n    struct Node* t = new Node;\r\n    t-&gt;val = value;\r\n    t-&gt;next = NULL;\r\n    return t;\r\n}\r\n\/\/ Driver Function\r\nint main()\r\n{\r\n    \/\/ Adding elements to the empty list\r\n    struct Node* head = newNode(5);\r\n    head-&gt;next = newNode(10);\r\n    head-&gt;next-&gt;next = newNode(15);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(20);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(25);\r\n    cout &lt;&lt; &quot;Original List&quot; &lt;&lt; endl;\r\n    display_List(head);\r\n    head = delete_middle(head);\r\n    cout &lt;&lt; &quot;List after deleting the middle element&quot;&lt;&lt; endl;\r\n    display_List(head);\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_4220_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\n\r\nclass DeleteMiddle {\r\n\r\n\t\/* Link list Node *\/\r\n\tstatic class Node {\r\n\t\tint data;\r\n\t\tNode next;\r\n\t}\r\n\r\n\t\/\/ Deletes middle node and returns head of the modified list\r\n\tstatic Node deleteMid(Node head)\r\n\t{\r\n\t\t\/\/ Base cases\r\n\t\tif (head == null)\r\n\t\t\treturn null;\r\n\t\tif (head.next == null) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\t\/\/ Initialize slow and fast pointers to reach middle of linked list\r\n\t\tNode slow_ptr = head;\r\n\t\tNode fast_ptr = head;\r\n\r\n\t\t\/\/ Find the middle and previous of middle.\r\n\t\tNode prev = null;\r\n\r\n\t\t\/\/ To store previous of slow_ptr\r\n\t\twhile (fast_ptr != null && fast_ptr.next != null) {\r\n\t\t\tfast_ptr = fast_ptr.next.next;\r\n\t\t\tprev = slow_ptr;\r\n\t\t\tslow_ptr = slow_ptr.next;\r\n\t\t}\r\n\r\n\t\t\/\/ Delete the middle node\r\n\t\tprev.next = slow_ptr.next;\r\n\r\n\t\treturn head;\r\n\t}\r\n\r\n\t\/\/ A utility function to print a given linked list\r\n\tstatic void printList(Node ptr)\r\n\t{\r\n\t\twhile (ptr != null) {\r\n\t\t\tSystem.out.print(ptr.data + \"->\");\r\n\t\t\tptr = ptr.next;\r\n\t\t}\r\n\t\tSystem.out.println(\"NULL\");\r\n\t}\r\n\r\n\t\/\/ Utility function to create a new node.\r\n\tstatic Node newNode(int data)\r\n\t{\r\n\t\tNode temp = new Node();\r\n\t\ttemp.data = data;\r\n\t\ttemp.next = null;\r\n\t\treturn temp;\r\n\t}\r\n\r\n\t\/* Driver code*\/\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\t\/* Start with the empty list *\/\r\n\t\tNode head = newNode(1);\r\n\t\thead.next = newNode(2);\r\n\t\thead.next.next = newNode(3);\r\n\t\thead.next.next.next = newNode(4);\r\n\r\n\t\tSystem.out.println(\"Given Linked List\");\r\n\t\tprintList(head);\r\n\r\n\t\thead = deleteMid(head);\r\n\r\n\t\tSystem.out.println(\"Linked List after deletion of middle\");\r\n\t\tprintList(head);\r\n\t}\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_4220_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\n# Node in a linked list\r\nclass Node:\r\n\t\r\n\tdef __init__(self, data):\r\n\t\t\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\n# Create and handle list operations\r\nclass LinkedList:\r\n\t\r\n\tdef __init__(self):\r\n\t\t\r\n\t\t# Head of the list\r\n\t\tself.head = None\r\n\r\n\t# Add new node to the list end\r\n\tdef addToList(self, data):\r\n\t\t\r\n\t\tnewNode = Node(data)\r\n\t\tif self.head is None:\r\n\t\t\tself.head = newNode\r\n\t\t\treturn\r\n\t\t\t\r\n\t\tlast = self.head\r\n\t\t\r\n\t\twhile last.next:\r\n\t\t\tlast = last.next\r\n\t\t\t\r\n\t\tlast.next = newNode\r\n\r\n\t# Returns the list in string format\r\n\tdef __str__(self):\r\n\t\t\r\n\t\tlinkedListStr = \"\"\r\n\t\ttemp = self.head\r\n\t\t\r\n\t\twhile temp:\r\n\t\t\tlinkedListStr += str(temp.data) + \"->\"\r\n\t\t\ttemp = temp.next\r\n\t\t\t\r\n\t\treturn linkedListStr + \"NULL\"\r\n\r\n\t# Method deletes middle node\r\n\tdef deleteMid(self):\r\n\r\n\t\t# Base cases\r\n\t\tif (self.head is None or\r\n\t\t\tself.head.next is None):\r\n\t\t\treturn\r\n\r\n\t\t# Initialize slow and fast pointers\r\n\t\t# to reach middle of linked list\r\n\t\tslow_Ptr = self.head\r\n\t\tfast_Ptr = self.head\r\n\r\n\t\t# Find the middle and previous of middle\r\n\t\tprev = None\r\n\r\n\t\t# To store previous of slow pointer\r\n\t\twhile (fast_Ptr is not None and\r\n\t\t\tfast_Ptr.next is not None):\r\n\t\t\tfast_Ptr = fast_Ptr.next.next\r\n\t\t\tprev = slow_Ptr\r\n\t\t\tslow_Ptr = slow_Ptr.next\r\n\r\n\t\t# Delete the middle node\r\n\t\tprev.next = slow_Ptr.next\r\n\r\n# Driver code\r\nlinkedList = LinkedList()\r\n\r\nlinkedList.addToList(5)\r\nlinkedList.addToList(10)\r\nlinkedList.addToList(15)\r\nlinkedList.addToList(20)\r\nlinkedList.addToList(25)\r\n\r\nprint(\"Original List\")\r\nprint(linkedList)\r\n\r\nlinkedList.deleteMid()\r\n\r\nprint(\"List after deleting the middle element\")\r\nprint(linkedList)\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_4220 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_4220 a\"),jQuery(\"#tab-content_4220\"));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>Original List\n5\u219210\u219215\u219220\u219225\u2192NULL\nList after deleting the middle element\n5\u219210\u219220\u219225\u2192NULL<\/code><\/pre>\n<p><strong>Time Complexity:<\/strong> O(n), only a single traversal of the linked list is required.<br \/>\n<strong>Space Complexity:<\/strong> O(1), as no extra space is being used to delete middle element of linked list.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn this article, we learned about how to delete the middle element of the linked list. First, we used the naive approach in which we first count the total number of nodes in the linked list and then delete the (N\/2)th node. This approach required 2 traversals. Next, we discussed the optimized approach which used the concept of slow and fast pointers to find the middle element and then deleted the <\/p>\n<h2>Frequently Asked Questions(FAQs) related to delete the middle node of linked list<\/h2>\n<p>Here are some Frequently Asked Questions related to Deletion of middle element of linked list are given below.<\/p>\n<p><strong>Ques 1. Can you delete middle element of linked list in constant time?<\/strong><br \/>\n<strong>Ans.<\/strong> No, it is not possible to delete middle element of linked list in constant time because you need to traverse the list to find the middle node.<\/p>\n<p><strong>Ques 2. What is lazy deletion?<\/strong><br \/>\n<strong>Ans.<\/strong> It\u2019s an alternative method to the standard deletion method. Generally, we delete elements logically, but here we delete the elements physically by marking them as deleted by using a boolean value.<\/p>\n<p><strong>Ques 3. What\u2019s the main drawback of a singly linked list?<\/strong><br \/>\n<strong>Ans.<\/strong> As we are aware of the structure of the linked list, the memory which is used is required more as a pointer stores the address of the next element.<\/p>\n<p><strong>Ques 4. What happens if you try to delete a node that is not in the linked list?<\/strong><br \/>\n<strong>Ans.<\/strong> If you try to delete a node that is not in the linked list, the list remains unchanged.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linked lists consist of interconnected nodes, with each node containing data and a pointer to the next node in the sequence. Removing the middle node of a linked list poses a significant challenge. Here, we will discuss how to delete middle of linked list in an efficient way. How to Delete the Middle Node of [&hellip;]<\/p>\n","protected":false},"author":3,"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-4217","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>Algorithm to delete the middle element in the linked list | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to delete the middle element of the Linked Lists. This blog explains the algorithm for deleting the middle element in 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\/delete-middle-of-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Algorithm to delete the middle element in the linked list | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to delete the middle element of the Linked Lists. This blog explains the algorithm for deleting the middle element in the linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/delete-middle-of-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-24T02:02:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-27T12:23:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.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\/delete-middle-of-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Delete Middle Node of Linked List\",\"datePublished\":\"2021-08-24T02:02:04+00:00\",\"dateModified\":\"2023-07-27T12:23:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/\"},\"wordCount\":1720,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/\",\"name\":\"Algorithm to delete the middle element in the linked list | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png\",\"datePublished\":\"2021-08-24T02:02:04+00:00\",\"dateModified\":\"2023-07-27T12:23:25+00:00\",\"description\":\"Learn the most efficient way to delete the middle element of the Linked Lists. This blog explains the algorithm for deleting the middle element in the linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-middle-of-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\":\"Delete Middle Node of 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\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Algorithm to delete the middle element in the linked list | Linked List | Prepbytes","description":"Learn the most efficient way to delete the middle element of the Linked Lists. This blog explains the algorithm for deleting the middle element in 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\/delete-middle-of-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Algorithm to delete the middle element in the linked list | Linked List | Prepbytes","og_description":"Learn the most efficient way to delete the middle element of the Linked Lists. This blog explains the algorithm for deleting the middle element in the linked list.","og_url":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-24T02:02:04+00:00","article_modified_time":"2023-07-27T12:23:25+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.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\/delete-middle-of-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Delete Middle Node of Linked List","datePublished":"2021-08-24T02:02:04+00:00","dateModified":"2023-07-27T12:23:25+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/"},"wordCount":1720,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/","name":"Algorithm to delete the middle element in the linked list | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png","datePublished":"2021-08-24T02:02:04+00:00","dateModified":"2023-07-27T12:23:25+00:00","description":"Learn the most efficient way to delete the middle element of the Linked Lists. This blog explains the algorithm for deleting the middle element in the linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645186647086-delete%20middle%20of%20linked%20list.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/delete-middle-of-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":"Delete Middle Node of 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\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4217","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=4217"}],"version-history":[{"count":9,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4217\/revisions"}],"predecessor-version":[{"id":17379,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4217\/revisions\/17379"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}