{"id":5472,"date":"2021-09-29T18:56:46","date_gmt":"2021-09-29T18:56:46","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5472"},"modified":"2022-04-21T21:46:53","modified_gmt":"2022-04-21T21:46:53","slug":"deletion-at-different-positions-in-a-circular-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/","title":{"rendered":"Deletion At Different Positions in A Circular Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>The linked list is one of the most important concepts to know while preparing for interviews. Having a good grasp of a linked list can be a huge plus point in coding interviews.<\/p>\n<\/p>\n<h3>Problem Statement<\/h3>\n<p>We will be given a circular linked list and a position, and we need to delete the element present at that position in the list.<\/p>\n<p><strong>Note:<\/strong> we need to follow 0 based indexing while deleting the nodes<\/p>\n<h3>Problem Statement Understanding<\/h3>\n<p>Let&#8217;s try to understand the problem with the help of an example.<\/p>\n<p>If the linked list given to us is:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-34.png\" alt=\"\" \/><\/p>\n<p>Now, if <strong>position = 0<\/strong>. <\/p>\n<ul>\n<li>Then, we need to delete the first node of the list, and after deletion, the list will now look like this:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-33.png\" alt=\"\" \/><\/li>\n<\/ul>\n<p>If the <strong>position = 2<\/strong>.<\/p>\n<ul>\n<li>Then, we need to delete the third node, and the list after deletion will look like this:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_3-19.png\" alt=\"\" \/><\/li>\n<\/ul>\n<p>At this point, we have understood the problem statement. Now we will try to formulate an approach for this problem.<\/p>\n<p>Before moving to the approach section, try to think about how you can approach this problem.<\/p>\n<ul>\n<li>If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.<\/li>\n<\/ul>\n<p>Let\u2019s move to the approach section.<\/p>\n<h3>Approach<\/h3>\n<p>We will divide our problem into three sections:<br \/>\n1) When <strong>position = 0<\/strong>:<br \/>\na) It means that we need to delete the first node of the circular linked list.<br \/>\nb) In this case, we need to reach the last node and then connect it with the second node.<br \/>\nc) After that, we will update the head pointer for the list.<\/p>\n<p>2) When <strong>position = length of the list<\/strong> (we need to delete the last node):<br \/>\na) In this case, we need to reach the last second node and connect it with the first node.<\/p>\n<p>3) When we need to delete a middle node:<br \/>\na) We need to keep track of node position while iterating the list.<br \/>\nb) when we find the node at the specified position, we need to connect its previous node with its next node.<\/p>\n<p>Let&#8217;s move to the algorithm section to see the above approach in more depth.<\/p>\n<h3>Algorithm<\/h3>\n<p>1) First, calculate the length of the list and save it in <strong>len<\/strong> variable.<br \/>\n2) Initialize a variable <strong>count<\/strong> by 1.<br \/>\n3) If the <strong>head<\/strong> is NULL, then it means that the list is empty, so return from the function.<br \/>\n4) If the <strong>position<\/strong> is less than <strong>0<\/strong> or greater than <strong>len<\/strong>, then return from the function.<br \/>\n5) If <strong>position == 0<\/strong> then, call <strong>deleteFirstNode function<\/strong>:<\/p>\n<ul>\n<li>Initialize <strong>curr<\/strong> and <strong>left<\/strong> with the head of the list.<\/li>\n<li>Return from the function if the <strong>head<\/strong> is NULL.<\/li>\n<li>Make the <strong>head<\/strong> NULL and return from the function, if <strong>left-&gt;next<\/strong> is <strong>left<\/strong> (this is the case when there is only 1 node in the list).<\/li>\n<li>Run a while loop till <strong>left-&gt;next<\/strong> is not equal to <strong>head<\/strong>.\n<ul>\n<li>Inside the loop, advance <strong>left<\/strong> by one node and update <strong>curr<\/strong> with the next node of <strong>left<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>Now, when the loop is executed completely, connect the next pointer of <strong>left<\/strong> to the next node of <strong>curr<\/strong>.<\/li>\n<li>Update the <strong>head<\/strong> node by <strong>left-&gt;next<\/strong>.<\/li>\n<li>Delete the old head node, i.e., <strong>curr<\/strong>.<br \/>\n6) If <strong>position == (len-1)<\/strong> then, call <strong>deleteLastNode function<\/strong>:<\/li>\n<li>Initialize the <strong>curr<\/strong> pointer with <strong>head<\/strong> and <strong>left<\/strong> with NULL.<\/li>\n<li>Return from the function if the <strong>head<\/strong> is NULL.<\/li>\n<li>Make the <strong>head<\/strong> NULL and return from the function, if <strong>curr-&gt;next<\/strong> is <strong>curr<\/strong> (this is the case when there is only 1 node in the list).<\/li>\n<li>Run a while loop till <strong>curr-&gt;next<\/strong> is not equal to <strong>head<\/strong>.\n<ul>\n<li>Inside the loop, save the value of <strong>curr<\/strong> in <strong>left<\/strong> and advance <strong>curr<\/strong> by one node.<\/li>\n<\/ul>\n<\/li>\n<li>Now, when the loop is executed completely, connect the next pointer of <strong>left<\/strong> to the next node of <strong>curr<\/strong>.<\/li>\n<li>Update the <strong>head<\/strong> node by <strong>left-&gt;next<\/strong>.<\/li>\n<li>Delete the old head node, i.e., <strong>curr<\/strong>.<br \/>\n7) Run a while loop till <strong>len<\/strong> is positive.<\/li>\n<li>If position is equal to <strong>count<\/strong> then,\n<ul>\n<li>Connect <strong>previous node\u2019s next pointer<\/strong> to <strong>current node\u2019s next pointer<\/strong>. <\/li>\n<li>Delete the <strong>curr<\/strong> pointer and return from the function.<\/li>\n<\/ul>\n<\/li>\n<li>Advance <strong>previous<\/strong> by one node and update <strong>curr<\/strong> to the node next to <strong>previous<\/strong>.<\/li>\n<li>Decrement <strong>len<\/strong> and increment <strong>count<\/strong> by one.<br \/>\n8) Return from the function once while loop breaks.<\/li>\n<\/ul>\n<h3>Dry Run<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_4-12.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_5-7.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_6_7.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5473 {\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_5473 .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_5473 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5473 .wpsm_nav-tabs > li.active > a, #tab_container_5473 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5473 .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_5473 .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_5473 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5473 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5473 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5473 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5473 .wpsm_nav-tabs > li > a:hover , #tab_container_5473 .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_5473 .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_5473 .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_5473 .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_5473 .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_5473 .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_5473 .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_5473 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5473 .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_5473 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5473 .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_5473 .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_5473\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5473\">\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_5473_1\" aria-controls=\"tabs_desc_5473_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_5473_2\" aria-controls=\"tabs_desc_5473_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_5473_3\" aria-controls=\"tabs_desc_5473_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_5473_4\" aria-controls=\"tabs_desc_5473_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_5473\">\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_5473_1\">\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 <bits\/stdc++.h>\r\nusing namespace std;\r\n\r\nclass Node {\r\n    public:\r\n    int data;\r\n    Node* next;\r\n    Node(int d)\r\n    {\r\n        data = d;\r\n        next = NULL;\r\n    }\r\n};\r\n\/\/ This function will print data of \r\n\/\/ all the nodes present in the list\r\nvoid Display(Node* head)\r\n{\r\n    Node* temp = head;\r\n \r\n    \/\/ In case of an empty list:\r\n    if (head == NULL) {\r\n        printf(\"&#92;nDisplay List is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    else {\r\n        do {\r\n            cout<<(temp->data);\r\n            temp = temp->next;\r\n            cout<<\" \";\r\n        } while (temp != head);\r\n    }\r\n    cout<<'&#92;n';\r\n}\r\n\r\n\/\/ This function calculates the \r\n\/\/ number of nodes in the list\r\nint Length(Node* head)\r\n{\r\n    Node* temp = head;\r\n    int len = 0;\r\n \r\n    \/\/ in case of an empty list, return 0\r\n    if (head == NULL) {\r\n        return 0;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    else {\r\n        do {\r\n            temp = temp->next;\r\n            len++;\r\n        } while (temp != head);\r\n    }\r\n    return len;\r\n}\r\n\r\n\/\/ This node deletes the first node\r\n\/\/ of the list\r\nvoid deleteFirstNode(Node** head)\r\n{\r\n    Node *left = *head, *curr = *head;\r\n \r\n    \/\/ if the list is empty\r\n    if (*head == NULL) {\r\n        printf(\"&#92;nList is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ In case, the list has a single node \r\n    if (left->next == left) {\r\n        *head = NULL;\r\n        return;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    while (left->next != *head) {\r\n \r\n        left = left->next;\r\n        curr = left->next;\r\n    }\r\n \r\n    \/\/ left will be pointing to the last node \r\n    \/\/ and curr will be the head of the list\r\n    \/\/ so, we will connect left with the curr->next\r\n    left->next = curr->next;\r\n \r\n    \/\/ make second node as head node\r\n    *head = left->next;\r\n    free(curr);\r\n \r\n    return;\r\n}\r\n\r\n\/\/ This function deletes the last\r\n\/\/ node of the list\r\nvoid deleteLastNode(Node** head)\r\n{\r\n    Node *curr = *head,  *left = NULL;\r\n \r\n    \/\/ if the list is empty\r\n    if (*head == NULL) {\r\n        printf(\"&#92;nList is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ In case, the list has a single node \r\n    if (curr->next == curr) {\r\n        *head = NULL;\r\n        return;\r\n    }\r\n \r\n    \/\/iterate the list \r\n    while (curr->next != *head) {\r\n        left = curr;\r\n        curr = curr->next;\r\n    }\r\n \r\n    left->next = curr->next;\r\n    *head = left->next;\r\n    free(curr);\r\n \r\n    return;\r\n}\r\n\r\nvoid deleteAtPosition(Node** head, int position)\r\n{\r\n    \/\/ find length of the list\r\n    int len = Length(*head);\r\n    int count = 1;\r\n    Node *previous = *head, *curr = *head;\r\n \r\n    \/\/ check if the list is empty\r\n    if (*head == NULL) {\r\n        printf(\"&#92;nDelete Last List is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ check if the given position is out of\r\n    \/\/ bound of the list\r\n    if (position >= len || position < 0) {\r\n        printf(\"&#92;nposition is not Found&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ first node deletion\r\n    if (position == 0) {\r\n        deleteFirstNode(head);\r\n        return;\r\n    }\r\n    \/\/ last node deletion\r\n    else if(position == len-1){\r\n        deleteLastNode(head);\r\n        return;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    while (len > 0) {\r\n \r\n        \/\/ delete the node when position is found\r\n        if (position == count) {\r\n            previous->next = curr->next;\r\n            free(curr);\r\n            return;\r\n        }\r\n        previous = previous->next;\r\n        curr = previous->next;\r\n        len--;\r\n        count++;\r\n    }\r\n    return;\r\n}\r\n\r\nint main()\r\n{\r\n    Node *head = new Node(3);\r\n    head->next = new Node(7);\r\n    head->next->next = new Node(2);\r\n    head->next->next->next = new Node(5);\r\n    head->next->next->next->next = head;\r\n    cout<<\"Original list \";\r\n    Display(head);\r\n    cout<<\"After deleting third node \";\r\n    deleteAtPosition(&head,2);\r\n    Display(head);\r\n    cout<<\"After deleting last node \";\r\n    deleteAtPosition(&head,Length(head)-1);\r\n    Display(head);\r\n    cout<<\"After deleting first node \";\r\n    deleteAtPosition(&head,0);\r\n    Display(head);\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_5473_2\">\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<stdio.h>\r\n#include<stdlib.h>\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n\/\/ This function will print data of \r\n\/\/ all the nodes present in the list\r\nvoid Display(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n \r\n    \/\/ In case of an empty list:\r\n    if (head == NULL) {\r\n        printf(\"&#92;nDisplay List is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    else {\r\n        do {\r\n            printf(\"%d\",temp->data);\r\n            temp = temp->next;\r\n            printf(\" \");\r\n        } while (temp != head);\r\n    }\r\n    printf(\"&#92;n\");\r\n}\r\n\/\/ This function calculates the \r\n\/\/ number of nodes in the list\r\nint Length(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    int len = 0;\r\n \r\n    \/\/ in case of an empty list, return 0\r\n    if (head == NULL) {\r\n        return 0;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    else {\r\n        do {\r\n            temp = temp->next;\r\n            len++;\r\n        } while (temp != head);\r\n    }\r\n    return len;\r\n}\r\n\/\/ This node deletes the first node\r\n\/\/ of the list\r\nvoid deleteFirstNode(struct Node** head)\r\n{\r\n    struct Node* left = *head; \r\n    struct Node *curr = *head;\r\n \r\n    \/\/ if the list is empty\r\n    if (*head == NULL) {\r\n        printf(\"&#92;nList is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ In case, the list has a single node \r\n    if (left->next == left) {\r\n        *head = NULL;\r\n        return;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    while (left->next != *head) {\r\n \r\n        left = left->next;\r\n        curr = left->next;\r\n    }\r\n \r\n    \/\/ left will be pointing to the last node \r\n    \/\/ and curr will be the head of the list\r\n    \/\/ so, we will connect left with the curr->next\r\n    left->next = curr->next;\r\n \r\n    \/\/ make second node as head node\r\n    *head = left->next;\r\n    free(curr);\r\n \r\n    return;\r\n}\r\n\/\/ This function deletes the last\r\n\/\/ node of the list\r\nvoid deleteLastNode(struct Node** head)\r\n{\r\n   struct Node *curr = *head,  *left = NULL;\r\n \r\n    \/\/ if the list is empty\r\n    if (*head == NULL) {\r\n        printf(\"&#92;nList is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ In case, the list has a single node \r\n    if (curr->next == curr) {\r\n        *head = NULL;\r\n        return;\r\n    }\r\n \r\n    \/\/iterate the list \r\n    while (curr->next != *head) {\r\n        left = curr;\r\n        curr = curr->next;\r\n    }\r\n \r\n    left->next = curr->next;\r\n    *head = left->next;\r\n    free(curr);\r\n \r\n    return;\r\n}\r\nvoid deleteAtPosition(struct Node** head, int position)\r\n{\r\n    \/\/ find length of the list\r\n    int len = Length(*head);\r\n    int count = 1;\r\n    struct Node *previous = *head, *curr = *head;\r\n \r\n    \/\/ check if the list is empty\r\n    if (*head == NULL) {\r\n        printf(\"&#92;nDelete Last List is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ check if the given position is out of\r\n    \/\/ bound of the list\r\n    if (position >= len || position < 0) {\r\n        printf(\"&#92;nposition is not Found&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ first node deletion\r\n    if (position == 0) {\r\n        deleteFirstNode(head);\r\n        return;\r\n    }\r\n    \/\/ last node deletion\r\n    else if(position == len-1){\r\n        deleteLastNode(head);\r\n        return;\r\n    }\r\n \r\n    \/\/ iterate the complete list\r\n    while (len > 0) {\r\n \r\n        \/\/ delete the node when position is found\r\n        if (position == count) {\r\n            previous->next = curr->next;\r\n            free(curr);\r\n            return;\r\n        }\r\n        previous = previous->next;\r\n        curr = previous->next;\r\n        len--;\r\n        count++;\r\n    }\r\n    return;\r\n}\r\n\r\nvoid push(struct Node** head_ref, int data)\r\n{\r\n    struct Node* ptr1 = (struct Node*)malloc(sizeof(struct Node));\r\n    ptr1->data = data;\r\n    ptr1->next = *head_ref;\r\n \r\n    if (*head_ref != NULL) {\r\n \r\n        struct Node* temp = *head_ref;\r\n        while (temp->next != *head_ref)\r\n            temp = temp->next;\r\n        temp->next = ptr1;\r\n    }\r\n    else\r\n        ptr1->next = ptr1; \/*For the first node *\/\r\n \r\n    *head_ref = ptr1;\r\n}\r\nint main()\r\n{\r\n    struct Node* head = NULL;\r\n    push(&head, 2);\r\n    push(&head, 5);\r\n    push(&head, 7);\r\n    push(&head, 8);\r\n    push(&head, 10);\r\n    printf(\"Original list \");\r\n    Display(head);\r\n    printf(\"After deleting third node \");\r\n    deleteAtPosition(&head,2);\r\n    Display(head);\r\n    printf(\"After deleting last node \");\r\n    deleteAtPosition(&head,Length(head)-1);\r\n    Display(head);\r\n    printf(\"After deleting first node \");\r\n    deleteAtPosition(&head,0);\r\n    Display(head);\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_5473_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 DeletetionAtPos\r\n{\r\n \r\n\/\/ structure for a node\r\nstatic class Node\r\n{\r\n    int data;\r\n    Node next;\r\n};\r\n \r\n\/\/ Function to insert a node at the end of\r\n\/\/ a Circular linked list\r\nstatic Node Insert(Node head, int data)\r\n{\r\n    Node current = head;\r\n     \r\n    \/\/ Create a new node\r\n    Node newNode = new Node();\r\n\r\n    \r\n \r\n    \/\/ insert data into newly created node\r\n    newNode.data = data;\r\n \r\n    \/\/ check list is empty\r\n    \/\/ if not have any node then\r\n    \/\/ make first node it\r\n    if (head == null)\r\n    {\r\n        newNode.next = newNode;\r\n        head = newNode;\r\n        return head;\r\n    }\r\n \r\n    \/\/ if list have already some node\r\n    else\r\n    {\r\n \r\n        \/\/ move first node to last node\r\n        while (current.next != head)\r\n        {\r\n            current = current.next;\r\n        }\r\n \r\n        \/\/ put first or head node address\r\n        \/\/ in new node link\r\n        newNode.next = head;\r\n \r\n        \/\/ put new node address into last\r\n        \/\/ node link(next)\r\n        current.next = newNode;\r\n    }\r\n    return head;\r\n}\r\n \r\n\/\/ Function print data of list\r\nstatic void Display( Node head)\r\n{\r\n    Node current = head;\r\n \r\n    \/\/ if list is empty, simply show message\r\n    if (head == null)\r\n    {\r\n        System.out.printf(\"&#92;nDisplay List is empty&#92;n\");\r\n        return;\r\n    }\r\n \r\n    \/\/ traverse first to last node\r\n    else\r\n    {\r\n        do\r\n        {\r\n            System.out.printf(\"%d \", current.data);\r\n            current = current.next;\r\n        } while (current != head);\r\n    }\r\n}\r\n \r\n\/\/ Function return number of nodes present in list\r\nstatic int Length(Node head)\r\n{\r\n    Node current = head;\r\n    int count = 0;\r\n \r\n    \/\/ if list is empty\r\n    \/\/ simply return length zero\r\n    if (head == null)\r\n    {\r\n        return 0;\r\n    }\r\n \r\n    \/\/ traverse first to last node\r\n    else\r\n    {\r\n        do\r\n        {\r\n            current = current.next;\r\n            count++;\r\n        } while (current != head);\r\n    }\r\n    return count;\r\n}\r\n \r\n\/\/ Function delete First node of\r\n\/\/ Circular Linked List\r\nstatic Node DeleteFirst(Node head)\r\n{\r\n    Node previous = head, next = head;\r\n \r\n    \/\/ check list have any node\r\n    \/\/ if not then return\r\n    if (head == null)\r\n    {\r\n        System.out.printf(\"&#92;nList is empty&#92;n\");\r\n        return null;\r\n    }\r\n \r\n    \/\/ check list have single node\r\n    \/\/ if yes then delete it and return\r\n    if (previous.next == previous)\r\n    {\r\n        head = null;\r\n        return null;\r\n    }\r\n \r\n    \/\/ traverse second to first\r\n    while (previous.next != head)\r\n    {\r\n        previous = previous.next;\r\n        next = previous.next;\r\n    }\r\n \r\n    \/\/ now previous is last node and\r\n    \/\/ next is first node of list\r\n    \/\/ first node(next) link address\r\n    \/\/ put in last node(previous) link\r\n    previous.next = next.next;\r\n \r\n    \/\/ make second node as head node\r\n    head = previous.next;\r\n \r\n    return head;\r\n}\r\n \r\n\/\/ Function to delete last node of\r\n\/\/ Circular Linked List\r\nstatic Node DeleteLast(Node head)\r\n{\r\n    Node current = head, previous=null;\r\n \r\n    \/\/ check if list doesn't have any node\r\n    \/\/ if not then return\r\n    if (head == null)\r\n    {\r\n        System.out.printf(\"&#92;nList is empty&#92;n\");\r\n        return null;\r\n    }\r\n \r\n    \/\/ check if list have single node\r\n    \/\/ if yes then delete it and return\r\n    if (current.next == current)\r\n    {\r\n        head = null;\r\n        return null;\r\n    }\r\n \r\n    \/\/ move first node to last\r\n    \/\/ previous\r\n    while (current.next != head)\r\n    {\r\n        previous = current;\r\n        current = current.next;\r\n    }\r\n \r\n    previous.next = current.next;\r\n    head = previous.next;\r\n     \r\n    return head;\r\n}\r\n \r\n\/\/ Function delete node at a given position\r\n\/\/ of Circular Linked List\r\nstatic Node DeleteAtPosition( Node head, int index)\r\n{\r\n    \/\/ Find length of list\r\n    int len = Length(head);\r\n    int count = 1;\r\n    Node previous = head, next = head;\r\n \r\n    \/\/ check list have any node\r\n    \/\/ if not then return\r\n    if (head == null)\r\n    {\r\n        System.out.printf(\"&#92;nDelete Last List is empty&#92;n\");\r\n        return null;\r\n    }\r\n \r\n    \/\/ given index is in list or not\r\n    if (index >= len || index < 0)\r\n    {\r\n        System.out.printf(\"&#92;nIndex is not Found&#92;n\");\r\n        return null;\r\n    }\r\n \r\n    \/\/ delete first node\r\n    if (index == 0)\r\n    {\r\n        head = DeleteFirst(head);\r\n        return head;\r\n    }\r\n \r\n    \/\/ traverse first to last node\r\n    while (len > 0)\r\n    {\r\n \r\n        \/\/ if index found delete that node\r\n        if (index == count)\r\n        {\r\n            previous.next = next.next;\r\n             \r\n            return head;\r\n        }\r\n        previous = previous.next;\r\n        next = previous.next;\r\n        len--;\r\n        count++;\r\n    }\r\n    return head;\r\n}\r\n \r\n\/\/ Driver Code\r\npublic static void main(String args[])\r\n{\r\n    Node head = null;\r\n    head = Insert(head, 99);\r\n    head = Insert(head, 11);\r\n    head = Insert(head, 22);\r\n    head = Insert(head, 33);\r\n    head = Insert(head, 44);\r\n    head = Insert(head, 55);\r\n    head = Insert(head, 66);\r\n \r\n    \/\/ Deleting Node at position\r\n    System.out.printf(\"Initial List: \");\r\n    Display(head);\r\n    System.out.printf(\"&#92;nAfter Deleting node at index 4: \");\r\n    head = DeleteAtPosition(head, 4);\r\n    Display(head);\r\n \r\n    \/\/ Deleting first Node\r\n    System.out.printf(\"&#92;n&#92;nInitial List: \");\r\n    Display(head);\r\n    System.out.printf(\"&#92;nAfter Deleting first node: \");\r\n    head = DeleteFirst(head);\r\n    Display(head);\r\n \r\n    \/\/ Deleting last Node\r\n    System.out.printf(\"&#92;n&#92;nInitial List: \");\r\n    Display(head);\r\n    System.out.printf(\"&#92;nAfter Deleting last node: \");\r\n    head = DeleteLast(head);\r\n    Display(head);\r\n}\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_5473_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\r\nclass Node:\r\n\tdef __init__(self, new_data):\r\n\t\tself.data = new_data\r\n\t\tself.next = None\r\n\t\tself.prev = None\r\n\r\n# This function to insert a node at the end of a list\r\ndef Insert(head, data):\r\n\r\n\tcurrent = head\r\n\t\r\n\tnewNode = Node(0)\r\n\r\n\tif (newNode == None):\r\n\t\r\n\t\tprint(\"&#92;nMemory Error&#92;n\")\r\n\t\treturn None\r\n\r\n\tnewNode.data = data\r\n\r\n\tif (head == None) :\r\n\t\tnewNode.next = newNode\r\n\t\thead = newNode\r\n\t\treturn head\r\n\r\n\telse:\r\n\r\n\t\twhile (current.next != head):\r\n\t\t\r\n\t\t\tcurrent = current.next\r\n\r\n\t\tnewNode.next = head\r\n\r\n\t\tcurrent.next = newNode\r\n\t\r\n\treturn head\r\n\r\n\r\n# This function will print data of all the nodes present in the list\r\ndef Display(head):\r\n\r\n\tcurrent = head\r\n\r\n\t# In case of an empty list:\r\n\tif (head == None):\r\n\t\r\n\t\tprint(\"&#92;nDisplay List is empty&#92;n\")\r\n\t\treturn\r\n\t\r\n\t# iterate the complete list\r\n\telse:\r\n\t\r\n\t\twhile(True):\r\n\t\t\tprint( current.data,end=\" \")\r\n\t\t\tcurrent = current.next\r\n\t\t\tif (current == head):\r\n\t\t\t\tbreak\r\n\t\r\n# This function calculates the number of nodes in the list\r\ndef Length(head):\r\n\tcurrent = head\r\n\tcount = 0\r\n\r\n\t# in case of an empty list, return 0\r\n\tif (head == None):\r\n\t\r\n\t\treturn 0\r\n\r\n\t# iterate the complete list\r\n\telse:\r\n\t\r\n\t\twhile(True):\r\n\t\t\tcurrent = current.next\r\n\t\t\tcount = count + 1\r\n\t\t\tif (current == head):\r\n\t\t\t\tbreak\r\n\t\r\n\treturn count\r\n\r\n# This node deletes the first node of the list\r\ndef deleteFirstNode(head):\r\n\tprevious = head\r\n\tnext = head\r\n\r\n\tif (head == None) :\r\n\t\tprint(\"&#92;nList is empty\")\r\n\t\treturn None\r\n\t\r\n\tif (previous.next == previous) :\r\n\t\r\n\t\thead = None\r\n\t\treturn None\r\n\t\r\n\twhile (previous.next != head):\r\n\t\tprevious = previous.next\r\n\t\tnext = previous.next\r\n\t\r\n\tprevious.next = next.next\r\n\r\n\thead = previous.next\r\n\r\n\treturn head\r\n\r\n# This function deletes the last node of the list\r\ndef DeleteLast(head):\r\n\tcurrent = head\r\n\ttemp = head\r\n\tprevious = None\r\n\r\n\tif (head == None):\r\n\t\tprint(\"&#92;nList is empty\")\r\n\t\treturn None\r\n\r\n\tif (current.next == current) :\r\n\t\thead = None\r\n\t\treturn None\r\n\r\n\twhile (current.next != head):\r\n\t\tprevious = current\r\n\t\tcurrent = current.next\r\n\t\r\n\tprevious.next = current.next\r\n\thead = previous.next\r\n\t\r\n\treturn head\r\n\r\n# This function delete node at a given position of List\r\ndef DeleteAtPosition(head, index):\r\n\r\n\t# Find length of list\r\n\tlen = Length(head)\r\n\tcount = 1\r\n\tprevious = head\r\n\tnext = head\r\n\r\n\t# check if the list is empty\r\n\tif (head == None):\r\n\t\tprint(\"&#92;nDelete Last List is empty\")\r\n\t\treturn None\r\n\t\r\n\t# check if the given position is out of bound of the list\r\n\tif (index >= len or index < 0) :\r\n\t\tprint(\"&#92;nPosition is not Found\")\r\n\t\treturn None\r\n\r\n\t# first node deletion\r\n\tif (index == 0) :\r\n\t\thead = deleteFirstNode(head)\r\n\t\treturn head\r\n\r\n\t# iterate the complete list\r\n\twhile (len > 0):\r\n\t\r\n\t\t# delete the node when position is found\r\n\t\tif (index == count):\r\n\t\t\tprevious.next = next.next\r\n\t\t\t\r\n\t\t\treturn head\r\n\t\t\r\n\t\tprevious = previous.next\r\n\t\tnext = previous.next\r\n\t\tlen = len - 1\r\n\t\tcount = count + 1\r\n\t\r\n\treturn head\r\n\r\n\r\nhead = None\r\nhead = Insert(head, 3)\r\nhead = Insert(head, 7)\r\nhead = Insert(head, 2)\r\nhead = Insert(head, 5)\r\n\r\nprint(\"Original List: \")\r\nDisplay(head)\r\nprint(\"&#92;nAfter deleting node at index 3: \")\r\nhead = DeleteAtPosition(head, 2)\r\nDisplay(head)\r\n\r\nprint(\"&#92;nAfter deleting last node: \")\r\nhead = DeleteLast(head)\r\nDisplay(head)\r\n\r\nprint(\"&#92;nAfter Deleting first node: \")\r\nhead = deleteFirstNode(head)\r\nDisplay(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_5473 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_5473 a\"),jQuery(\"#tab-content_5473\"));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<h4>Output<\/h4>\n<p>Original list 3 7 2 5<br \/>\nAfter deleting third node 3 7 5<br \/>\nAfter deleting last node 3 7<br \/>\nAfter deleting first node 7<\/p>\n<p><strong>Time Complexity:<\/strong> O(n), where n is the number of nodes in the list<br \/>\n[forminator_quiz id=&#8221;5474&#8243;]<\/p>\n<p>So, in this blog, we have tried to explain how you can delete nodes at different positions in a circular list in the most optimal way.  If you want to practice more questions on linked lists, feel free to solve them at <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Prepbytes (Linked Lists)<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The linked list is one of the most important concepts to know while preparing for interviews. Having a good grasp of a linked list can be a huge plus point in coding interviews. Problem Statement We will be given a circular linked list and a position, and we need to delete the element present [&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-5472","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>Deletion At Different Positions in A Circular Linked List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn to delete nodes present at different positions in a circular 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\/deletion-at-different-positions-in-a-circular-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deletion At Different Positions in A Circular Linked List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn to delete nodes present at different positions in a circular linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-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-09-29T18:56:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-21T21:46:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Deletion At Different Positions in A Circular Linked List\",\"datePublished\":\"2021-09-29T18:56:46+00:00\",\"dateModified\":\"2022-04-21T21:46:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/\"},\"wordCount\":792,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/\",\"name\":\"Deletion At Different Positions in A Circular Linked List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png\",\"datePublished\":\"2021-09-29T18:56:46+00:00\",\"dateModified\":\"2022-04-21T21:46:53+00:00\",\"description\":\"Learn to delete nodes present at different positions in a circular linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-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\":\"Deletion At Different Positions in A Circular 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":"Deletion At Different Positions in A Circular Linked List | Linked List | Prepbytes","description":"Learn to delete nodes present at different positions in a circular 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\/deletion-at-different-positions-in-a-circular-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Deletion At Different Positions in A Circular Linked List | Linked List | Prepbytes","og_description":"Learn to delete nodes present at different positions in a circular linked list.","og_url":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-29T18:56:46+00:00","article_modified_time":"2022-04-21T21:46:53+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Deletion At Different Positions in A Circular Linked List","datePublished":"2021-09-29T18:56:46+00:00","dateModified":"2022-04-21T21:46:53+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/"},"wordCount":792,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/","name":"Deletion At Different Positions in A Circular Linked List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png","datePublished":"2021-09-29T18:56:46+00:00","dateModified":"2022-04-21T21:46:53+00:00","description":"Learn to delete nodes present at different positions in a circular linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011310332-Article_180.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/deletion-at-different-positions-in-a-circular-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":"Deletion At Different Positions in A Circular 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\/5472","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=5472"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5472\/revisions"}],"predecessor-version":[{"id":8051,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5472\/revisions\/8051"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}