{"id":4232,"date":"2021-08-24T12:10:04","date_gmt":"2021-08-24T12:10:04","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4232"},"modified":"2022-11-10T12:30:44","modified_gmt":"2022-11-10T12:30:44","slug":"move-all-occurrences-of-an-element-to-end-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/","title":{"rendered":"Move all occurrences of an element to end in a linked list"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png\" alt=\"\" \/><\/p>\n<p>In this article we will learn how to move all occurrences of an element to end in a linked list. As we all know Linked list is a linear data structure in which elements are linked using pointers and are not stored in contiguous memory locations. Let&#8217;s try to understand how to move all occurrences of an element to end in a linked list.<\/p>\n<h3>Problem Statement<\/h3>\n<p>In this problem, we are given a linked list and a key in it. Our task is to move all the occurrences of the given key to the end of the linked list while maintaining the order of all other elements the same.<\/p>\n<h3>Problem Statement Understanding<\/h3>\n<p>Let\u2019s try to understand how we can move all occurrences of an element to end in a linked list with help of examples.<\/p>\n<p>Let\u2019s say, we are given a linked list:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-1-11.png\" alt=\"\" ><\/p>\n<p>Then according to the problem statement, we have to move all the occurrences of the key to the end of the linked list, and also we have to maintain the order of all other elements except the key same as their order in the original given linked list. So after moving all the occurrences of the key to the end, our final resultant linked list would look something like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-1-1.png\" alt=\"\" \/><\/p>\n<p>While forming the resultant linked list, first all the other elements except B will come in the same order as they were in the original linked list forming A\u2192C\u2192C and then all the occurrences of the B will be appended at the end of A\u2192C\u2192C forming A\u2192C\u2192C\u2192B\u2192B\u2192B.<\/p>\n<p><strong>Explanation:<\/strong> In our final resultant linked list, we can see that all the occurrence of the key B has been moved to the end of the linked list, preserving the order of the rest of the elements of the list. <\/p>\n<h4>Some other examples<\/h4>\n<p><strong>Input:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-2-9.png\" alt=\"\" \/><\/p>\n<p><strong>Output:<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-2-2.png\" alt=\"\" \/><\/p>\n<p><strong>Input:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-3-2.png\" alt=\"\" \/><\/p>\n<p><strong>Output:<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-3-1.png\" alt=\"\" \/><\/p>\n<p>Now I think from the above examples it is clear how we can move all occurrences of an element to end in a linked list. So next we have to think about how we can approach this problem towards a solution?<\/p>\n<p>Let\u2019s have a glance at the approach to move all occurrences of an element to end in a linked list.<\/p>\n<h3>Approach 1 to move all occurrences of an element to end in a linked list<\/h3>\n<p>A simple approach is the brute force approach. <\/p>\n<p>In this approach, we will be finding every occurrence of the given key element in the list. <\/p>\n<ul>\n<li>For each occurrence of the given key element in the list, we move it to the end of the linked list.<\/li>\n<\/ul>\n<p>This way, all occurrences of the given key element will be moved to the end of the linked list, and we will be successful in achieving our objective of moving all the occurrences of the given key to the end of the linked list.<\/p>\n<p><strong>Time Complexity:<\/strong> The worst case complexity of this brute force approach is O(N*N), where N is the total number of nodes in the linked list, as for each occurrence of the key element in the list, we take approx O(N) time for the traversal to the end of the linked list and then appending the key at the end.<\/p>\n<p><strong>Space Complexity:<\/strong> O(1), as no additional space is used.<\/p>\n<p>Although we were able to achieve our objective still the time complexity was of order N<sup>2<\/sup>. So now we will try to think about how can we reduce this complexity. Is a lesser time complexity solution possible? If possible, what do we have to use to convert our above O(N<sup>2<\/sup>) solution into an efficient lesser time complexity solution?<\/p>\n<p>Now in the next approach to move all occurrences of an element to end in a linked list we will see how using pointers we can reduce the complexity.<\/p>\n<h3>Approach 2 to move all occurrences of an element to end in a linked list<\/h3>\n<p>The time complexity of the previous approach can be optimized if we make use of 2 pointers. <\/p>\n<p>Let\u2019s say the 2 pointers are p1 and p2. <\/p>\n<ul>\n<li>p1 is the pointer to traverse the whole list one by one.<\/li>\n<li>p2 is the pointer to an occurrence of the key if a key is found, else it will be the same as p1.<\/li>\n<\/ul>\n<p>The algorithm to move all occurrences of the key element to the end is explained below.<\/p>\n<h3>Algorithm:<\/h3>\n<ul>\n<li>We will start both pointers from the head of the linked list.<\/li>\n<li>We move p2 only when p2 is not pointing to a key. Furthermore, we always move p1 until <strong>p1!=NULL<\/strong>.<\/li>\n<li>So, when p1 and p2 are not the same, we must have found a key that lies before p1. Therefore, we swap p1 and p2 and move p2 to the next location.<\/li>\n<li>The loop invariant is, after swapping data, all node values from p2 to p1 are equal to key.<\/li>\n<li>Finally, when <strong>p1==NULL<\/strong> we will have our result, our linked list will have all the occurrences of the key at the end of the linked list.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> For a better understanding go through the dry run step by step using the algorithm and code to move all occurrences of an element to end in a linked list, hopefully, you will get to know better what we are doing in the algorithm.<\/p>\n<h3>Dry Run to move all occurrences of an element to end in a linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Move-all-occurrences-of-an-element-to-end-in-a-linked-list-3.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Move-all-occurrences-of-an-element-to-end-in-a-linked-list-2.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation to move all occurrences of an element to end in a linked list<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4233 {\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_4233 .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_4233 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4233 .wpsm_nav-tabs > li.active > a, #tab_container_4233 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4233 .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_4233 .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_4233 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4233 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4233 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4233 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4233 .wpsm_nav-tabs > li > a:hover , #tab_container_4233 .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_4233 .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_4233 .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_4233 .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_4233 .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_4233 .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_4233 .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_4233 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4233 .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_4233 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4233 .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_4233 .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_4233\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4233\">\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_4233_1\" aria-controls=\"tabs_desc_4233_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_4233_2\" aria-controls=\"tabs_desc_4233_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_4233_3\" aria-controls=\"tabs_desc_4233_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_4233_4\" aria-controls=\"tabs_desc_4233_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_4233\">\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_4233_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n \r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\nstruct Node* newNode(int a)\r\n{\r\n    struct Node* temp =\r\n            (struct Node*) malloc(sizeof(struct Node));\r\n    temp-&gt;data = a;\r\n    temp-&gt;next = NULL;\r\n}\r\n \r\nvoid printList(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    while (temp != NULL) {\r\n        printf(&quot;%d&quot;,&amp;temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n \r\nvoid moveToEnd(struct Node* head, int key)\r\n{\r\n    struct Node* p1 = head;\r\n    struct Node* p2 = head;\r\n \r\n    while (p1 != NULL) {\r\n            if (p1 != p2 &amp;&amp; p1-&gt;data != key) {\r\n            p2-&gt;data = p1-&gt;data;\r\n            p1-&gt;data = key;\r\n            p2 = p2-&gt;next;\r\n        }\r\n \r\n        if (p2-&gt;data != key)\r\n            p2 = p2-&gt;next;\r\n \r\n        p1 = p1-&gt;next;\r\n    }\r\n}\r\n \r\nint main()\r\n{\r\n    struct Node* head = newNode(5);\r\n    head-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(7);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n \r\n    printf(&quot;Before moveToEnd(), the Linked list is&#92;n&quot;);\r\n    printList(head);\r\n \r\n    int key = 2;\r\n    moveToEnd(head, key);\r\n \r\n    printf(&quot;&#92;nAfter moveToEnd(), the Linked list is&#92;n&quot;);\r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4233_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits stdc++.h&gt;\r\nusing namespace std;\r\n \r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\nstruct Node* newNode(int a)\r\n{\r\n    Node* temp = new Node;\r\n    temp-&gt;data = a;\r\n    temp-&gt;next = NULL;\r\n}\r\n \r\nvoid printList(Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    while (temp != NULL) {\r\n        cout &lt;&lt; temp-&gt;data;\r\n        temp = temp-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n \r\nvoid moveToEnd(Node* head, int key)\r\n{\r\n    struct Node* p1 = head;\r\n    struct Node* p2 = head;\r\n \r\n    while (p1 != NULL) {\r\n            if (p1 != p2 &amp;&amp; p1-&gt;data != key) {\r\n            p2-&gt;data = p1-&gt;data;\r\n            p1-&gt;data = key;\r\n            p2 = p2-&gt;next;\r\n        }\r\n \r\n        if (p2-&gt;data != key)\r\n            p2 = p2-&gt;next;\r\n \r\n        p1 = p1-&gt;next;\r\n    }\r\n}\r\n \r\nint main()\r\n{\r\n    Node* head = newNode(5);\r\n    head-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(7);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n \r\n    printf(&quot;Before moveToEnd(), the Linked list is&#92;n&quot;);\r\n    printList(head);\r\n \r\n    int key = 2;\r\n    moveToEnd(head, key);\r\n \r\n    printf(&quot;&#92;nAfter moveToEnd(), the Linked list is&#92;n&quot;);\r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4233_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 MoveEndI {\r\n\r\n\tstatic class Node {\r\n\t\tint data;\r\n\t\tNode next;\r\n\t}\r\n\tstatic Node newNode(int x)\r\n\t{\r\n\t\tNode temp = new Node();\r\n\t\ttemp.data = x;\r\n\t\ttemp.next = null;\r\n\t\treturn temp;\r\n    }\r\n\tstatic void printList(Node head)\r\n\t{\r\n\t\tNode temp = head;\r\n\t\twhile (temp != null) {\r\n\t\t\tSystem.out.printf(&quot;%d &quot;, temp.data);\r\n\t\t\ttemp = temp.next;\r\n\t\t}\r\n\t\tSystem.out.printf(&quot;&#92;n&quot;);\r\n\t}\r\n\t\/\/ Moves all occurrences of given key to\r\n\t\/\/ end of linked list.\r\n\tstatic void moveToEnd(Node head, int key)\r\n\t{\r\n\t\t\/\/ Keeps track of locations where key\r\n\t\t\/\/ is present.\r\n\t\tNode pKey = head;\r\n\r\n\t\t\/\/ Traverse list\r\n\t\tNode pCrawl = head;\r\n\t\twhile (pCrawl != null) {\r\n\t\t\t\/\/ If current pointer is not same as pointer\r\n\t\t\t\/\/ to a key location, then we must have found\r\n\t\t\t\/\/ a key in linked list. We swap data of pCrawl\r\n\t\t\t\/\/ and pKey and move pKey to next position.\r\n\t\t\tif (pCrawl != pKey &amp;&amp; pCrawl.data != key) {\r\n\t\t\t\tpKey.data = pCrawl.data;\r\n\t\t\t\tpCrawl.data = key;\r\n\t\t\t\tpKey = pKey.next;\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ Find next position where key is present\r\n\t\t\tif (pKey.data != key)\r\n\t\t\t\tpKey = pKey.next;\r\n\r\n\t\t\t\/\/ Moving to next Node\r\n\t\t\tpCrawl = pCrawl.next;\r\n\t\t}\r\n\t}\r\n\t\/\/ Driver code\r\n\tpublic static void main(String args[])\r\n\t{\r\n\t\tNode head = newNode(10);\r\n\t\thead.next = newNode(20);\r\n\t\thead.next.next = newNode(10);\r\n\t\thead.next.next.next = newNode(30);\r\n\t\thead.next.next.next.next = newNode(40);\r\n\t\thead.next.next.next.next.next = newNode(10);\r\n\t\thead.next.next.next.next.next.next = newNode(60);\r\n\r\n\t\tSystem.out.printf(&quot;Before moveToEnd(), the Linked list is&#92;n&quot;);\r\n\t\tprintList(head);\r\n\r\n\t\tint key = 10;\r\n\t\tmoveToEnd(head, key);\r\n\r\n\t\tSystem.out.printf(&quot;&#92;nAfter moveToEnd(), the Linked list is&#92;n&quot;);\r\n\t\tprintList(head);\r\n\t}\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4233_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\ndef newNode(x):\r\n\r\n\ttemp = Node(0)\r\n\ttemp.data = x\r\n\ttemp.next = None\r\n\treturn temp\r\n\r\ndef printList( head):\r\n\r\n\ttemp = head\r\n\twhile (temp != None) :\r\n\t\tprint( temp.data,end = &quot; &quot;)\r\n\t\ttemp = temp.next\r\n\t\r\n\tprint()\r\n\r\ndef moveToEnd(head, key):\r\n\r\n\tpKey = head\r\n\tpCrawl = head\r\n\twhile (pCrawl != None) :\r\n\t\t\r\n\t\tif (pCrawl != pKey and pCrawl.data != key) :\r\n\t\t\tpKey.data = pCrawl.data\r\n\t\t\tpCrawl.data = key\r\n\t\t\tpKey = pKey.next\r\n\t\t\r\n\t\tif (pKey.data != key):\r\n\t\t\tpKey = pKey.next\r\n\r\n\t\tpCrawl = pCrawl.next\r\n\t\r\n\treturn head\r\n\r\nhead = newNode(5)\r\nhead.next = newNode(2)\r\nhead.next.next = newNode(2)\r\nhead.next.next.next = newNode(7)\r\nhead.next.next.next.next = newNode(2)\r\nhead.next.next.next.next.next = newNode(2)\r\nhead.next.next.next.next.next.next = newNode(2)\r\n\r\nprint(&quot;Before moveToEnd(), the Linked list is&quot;)\r\nprintList(head)\r\n\r\nkey = 2\r\nhead = moveToEnd(head, key)\r\n\r\nprint(&quot;After moveToEnd(), the Linked list is&quot;)\r\nprintList(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\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_4233 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_4233 a\"),jQuery(\"#tab-content_4233\"));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<h3>Output<\/h3>\n<p>Before moveToEnd(), the Linked list is<br \/>\n5 2 2 7 2 2 2<br \/>\nAfter moveToEnd(), the Linked list is<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-4-1.png\" alt=\"\" \/><\/p>\n<p><strong>Time Complexity:<\/strong> O(N), as we are traversing the list only once.<br \/>\n<strong>Space Complexity:<\/strong> O(1), as no additional space is used.<\/p>\n<h3>Approach 3 to move all occurrences of an element to end in a linked list<\/h3>\n<p>Although the previous approach is an efficient one, now we will discuss one more efficient approach. <\/p>\n<p>In this approach, we will make use of a pointer at the tail of the linked list. While traversing the list: <\/p>\n<ul>\n<li>If we find a node whose value is equal to the key, we will move that node to the last using that pointer tail.<\/li>\n<\/ul>\n<h3>Algorithm to move all occurrences of an element to end in a linked list<\/h3>\n<ul>\n<li>First we will create a new pointer last, then we will traverse the linked list to the end and will make this pointer&#8217;s last point to the tail of the linked list (i.e. last node of the linked list).<\/li>\n<li>Now again while iterating over the list starting from the head using a pointer current, we will check for every node:<br \/>\n1) If current \u2192 data = key, then we will move the node to the end of the linked list.<br \/>\n2) Else we move to the next location.<\/li>\n<li>Finally after the iteration is over, all the occurrences of the key will be at the end of the linked list.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> For a better understanding go through the dry run step by step using the algorithm and code, hopefully, you will get to know better what we are doing in the algorithm.<\/p>\n<h3>Dry Run to move all occurrences of an element to end in a linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Move-all-occurrences-of-an-element-to-end-in-a-linked-list-3.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Move-all-occurrences-of-an-element-to-end-in-a-linked-list-4.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Move-all-occurrences-of-an-element-to-end-in-a-linked-list-5.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation to move all occurrences of an element to end in a linked list<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4235 {\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_4235 .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_4235 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4235 .wpsm_nav-tabs > li.active > a, #tab_container_4235 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4235 .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_4235 .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_4235 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4235 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4235 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4235 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4235 .wpsm_nav-tabs > li > a:hover , #tab_container_4235 .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_4235 .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_4235 .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_4235 .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_4235 .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_4235 .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_4235 .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_4235 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4235 .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_4235 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4235 .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_4235 .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_4235\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4235\">\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_4235_1\" aria-controls=\"tabs_desc_4235_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_4235_2\" aria-controls=\"tabs_desc_4235_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_4235_3\" aria-controls=\"tabs_desc_4235_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_4235_4\" aria-controls=\"tabs_desc_4235_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_4235\">\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_4235_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n \r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\nstruct Node* newNode(int a)\r\n{\r\n    struct Node* temp =\r\n            (struct Node*) malloc(sizeof(struct Node));\r\n    temp-&gt;data = a;\r\n    temp-&gt;next = NULL;\r\n}\r\n \r\nstruct Node *keyToEnd(struct Node* head, int key)\r\n{\r\n    struct Node* tail = head;\r\n    if (head == NULL)\r\n    {\r\n        return NULL;\r\n    }\r\n    while (tail-&gt;next != NULL)\r\n    {\r\n        tail = tail-&gt;next;\r\n    }\r\n     \r\n    \r\n    struct Node* last = tail;\r\n    struct Node* current = head;\r\n    struct Node* prev = NULL;\r\n     \r\n    struct Node* prev2 = NULL;\r\n     \r\n    while (current != tail)\r\n    {\r\n        if (current-&gt;data == key &amp;&amp; prev2 == NULL)\r\n        {\r\n            prev = current;\r\n            current = current-&gt;next;\r\n            head = current;\r\n            last-&gt;next = prev;\r\n            last = last-&gt;next;\r\n            last-&gt;next = NULL;\r\n            prev = NULL;\r\n        }\r\n        else\r\n        {\r\n            if (current-&gt;data == key &amp;&amp; prev2 != NULL)\r\n            {\r\n                prev = current;\r\n                current = current-&gt;next;\r\n                prev2-&gt;next = current;\r\n                last-&gt;next = prev;\r\n                last = last-&gt;next;\r\n                last-&gt;next = NULL;\r\n            }\r\n            else if (current != tail)\r\n            {\r\n                prev2 = current;\r\n                current = current-&gt;next;\r\n            }\r\n        }\r\n    }\r\n    return head;\r\n}\r\n \r\nvoid printList(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    while (temp != NULL)\r\n    {\r\n        printf(&quot;%d &quot;,&amp;temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n\r\nint main()\r\n{\r\n    struct Node* root = newNode(5);\r\n    root-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next-&gt;next = newNode(7);\r\n    root-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n \r\n    int key = 2;\r\n    printf(&quot;Linked List before operations :&quot;);\r\n    printList(root);\r\n    printf(&quot;&#92;nLinked List after operations :&quot;);\r\n    root = keyToEnd(root, key);\r\n    printList(root);\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_4235_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits stdc++.h&gt;\r\nusing namespace std;\r\n \r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\nstruct Node* newNode(int a)\r\n{\r\n    Node* temp = new Node;\r\n    temp-&gt;data = a;\r\n    temp-&gt;next = NULL;\r\n}\r\n \r\nNode *keyToEnd(Node* head, int key)\r\n{\r\n    Node* tail = head;\r\n    if (head == NULL)\r\n    {\r\n        return NULL;\r\n    }\r\n    while (tail-&gt;next != NULL)\r\n    {\r\n        tail = tail-&gt;next;\r\n    }\r\n     \r\n    \r\n    Node* last = tail;\r\n    Node* current = head;\r\n    Node* prev = NULL;\r\n     \r\n    Node* prev2 = NULL;\r\n     \r\n    while (current != tail)\r\n    {\r\n        if (current-&gt;data == key &amp;&amp; prev2 == NULL)\r\n        {\r\n            prev = current;\r\n            current = current-&gt;next;\r\n            head = current;\r\n            last-&gt;next = prev;\r\n            last = last-&gt;next;\r\n            last-&gt;next = NULL;\r\n            prev = NULL;\r\n        }\r\n        else\r\n        {\r\n            if (current-&gt;data == key &amp;&amp; prev2 != NULL)\r\n            {\r\n                prev = current;\r\n                current = current-&gt;next;\r\n                prev2-&gt;next = current;\r\n                last-&gt;next = prev;\r\n                last = last-&gt;next;\r\n                last-&gt;next = NULL;\r\n            }\r\n            else if (current != tail)\r\n            {\r\n                prev2 = current;\r\n                current = current-&gt;next;\r\n            }\r\n        }\r\n    }\r\n    return head;\r\n}\r\n \r\nvoid printList(Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    while (temp != NULL)\r\n    {\r\n        cout &lt;&lt; temp-&gt;data;\r\n        temp = temp-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n\r\nint main()\r\n{\r\n    Node* root = newNode(5);\r\n    root-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next-&gt;next = newNode(7);\r\n    root-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    root-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n \r\n    int key = 2;\r\n    cout &lt;&lt; &quot;Linked List before operations :&quot;;\r\n    printList(root);\r\n    cout &lt;&lt; &quot;&#92;nLinked List after operations :&quot;;\r\n    root = keyToEnd(root, key);\r\n    printList(root);\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_4235_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 Node {\r\n\tint data;\r\n\tNode next;\r\n\r\n\tpublic Node(int data)\r\n\t{\r\n\t\tthis.data = data;\r\n\t\tthis.next = null;\r\n\t}\r\n}\r\n\r\nclass MoveEndIII {\r\n\r\n\tstatic Node root;\r\n\r\n\t\/\/ Function to remove key to end\r\n\tpublic static Node keyToEnd(Node head, int key)\r\n\t{\r\n\r\n\t\t\/\/ Node to keep pointing to tail\r\n\t\tNode tail = head;\r\n\r\n\t\tif (head == null) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\r\n\t\twhile (tail.next != null) {\r\n\t\t\ttail = tail.next;\r\n\t\t}\r\n\r\n\t\t\/\/ Node to point to last of linked list\r\n\t\tNode last = tail;\r\n\r\n\t\tNode current = head;\r\n\t\tNode prev = null;\r\n\r\n\t\t\/\/ Node prev2 to point to previous when head.data!=key\r\n\t\tNode prev2 = null;\r\n\r\n\t\t\/\/ loop to perform operations to remove key to end\r\n\t\twhile (current != tail) {\r\n\t\t\tif (current.data == key &amp;&amp; prev2 == null) {\r\n\t\t\t\tprev = current;\r\n\t\t\t\tcurrent = current.next;\r\n\t\t\t\thead = current;\r\n\t\t\t\tlast.next = prev;\r\n\t\t\t\tlast = last.next;\r\n\t\t\t\tlast.next = null;\r\n\t\t\t\tprev = null;\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tif (current.data == key &amp;&amp; prev2 != null) {\r\n\t\t\t\t\tprev = current;\r\n\t\t\t\t\tcurrent = current.next;\r\n\t\t\t\t\tprev2.next = current;\r\n\t\t\t\t\tlast.next = prev;\r\n\t\t\t\t\tlast = last.next;\r\n\t\t\t\t\tlast.next = null;\r\n\t\t\t\t}\r\n\t\t\t\telse if (current != tail) {\r\n\t\t\t\t\tprev2 = current;\r\n\t\t\t\t\tcurrent = current.next;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn head;\r\n\t}\r\n\t\/\/ Function to display linked list\r\n\tpublic static void display(Node root)\r\n\t{\r\n\t\twhile (root != null) {\r\n\t\t\tSystem.out.print(root.data + &quot; &quot;);\r\n\t\t\troot = root.next;\r\n\t\t}\r\n\t}\r\n\t\/\/ Driver Code\r\n\tpublic static void main(String args[])\r\n\t{\r\n\t\troot = new Node(5);\r\n\t\troot.next = new Node(2);\r\n\t\troot.next.next = new Node(2);\r\n\t\troot.next.next.next = new Node(7);\r\n\t\troot.next.next.next.next = new Node(2);\r\n\t\troot.next.next.next.next.next = new Node(2);\r\n\t\troot.next.next.next.next.next.next = new Node(2);\r\n\r\n\t\tint key = 2;\r\n\t\tSystem.out.println(&quot;Linked List before operations :&quot;);\r\n\t\tdisplay(root);\r\n\t\tSystem.out.println(&quot;&#92;nLinked List after operations :&quot;);\r\n\t\troot = keyToEnd(root, key);\r\n\t\tdisplay(root);\r\n\t}\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4235_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\ndef newNode(x):\r\n\r\n\ttemp = Node(0)\r\n\ttemp.data = x\r\n\ttemp.next = None\r\n\treturn temp\r\n\r\ndef printList( head):\r\n\r\n\ttemp = head\r\n\twhile (temp != None) :\r\n\t\tprint( temp.data,end = &quot; &quot;)\r\n\t\ttemp = temp.next\r\n\t\r\n\tprint()\r\n\r\ndef keyToEnd(head, key):\r\n\r\n\ttail = head\r\n\tif (head == None):\r\n\t\r\n\t\treturn None\r\n\t\r\n\twhile (tail.next != None):\r\n\t\r\n\t\ttail = tail.next \r\n\t\r\n\tlast = tail\r\n\tcurrent = head\r\n\tprev = None \r\n\tprev2 = None\r\n\t \r\n\twhile (current != tail):\r\n\t\r\n\t\tif (current.data == key and prev2 == None):\r\n\t\t\r\n\t\t\tprev = current\r\n\t\t\tcurrent = current.next\r\n\t\t\thead = current\r\n\t\t\tlast.next = prev\r\n\t\t\tlast = last.next\r\n\t\t\tlast.next = None\r\n\t\t\tprev = NULL\r\n\t\t\r\n\t\telse:\r\n\t\t\r\n\t\t\tif (current.data == key and prev2 != None):\r\n\t\t\t\r\n\t\t\t\tprev = current\r\n\t\t\t\tcurrent = current.next\r\n\t\t\t\tprev2.next = current\r\n\t\t\t\tlast.next = prev\r\n\t\t\t\tlast = last.next\r\n\t\t\t\tlast.next = None\r\n\t\t\t\r\n\t\t\telif (current != tail):\r\n\t\t\t\r\n\t\t\t\tprev2 = current\r\n\t\t\t\tcurrent = current.next\r\n\t\t\t\r\n\t\t\r\n\t\r\n\treturn head\r\n\r\nhead = newNode(5)\r\nhead.next = newNode(2)\r\nhead.next.next = newNode(2)\r\nhead.next.next.next = newNode(7)\r\nhead.next.next.next.next = newNode(2)\r\nhead.next.next.next.next.next = newNode(2)\r\nhead.next.next.next.next.next.next = newNode(2)\r\n\r\nprint(&quot;Linked List before operations :&quot;)\r\nprintList(head)\r\n\r\nkey = 2\r\nhead = keyToEnd(head, key)\r\n\r\nprint(&quot;Linked List after operations :&quot;)\r\nprintList(head)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4235 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_4235 a\"),jQuery(\"#tab-content_4235\"));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>Linked List before operations:<br \/>\n5 2 2 7 2 2 2<br \/>\nLinked List after operations:<br \/>\n5 7 2 2 2 2 2<\/p>\n<p><strong>Time Complexity:<\/strong> O(N), as we are traversing the list twice.<\/p>\n<p>So, in this article, you have learnt how to move all occurrences of an element to end in a linked list. In this we have explained three approaches with a complete picturised dry run and also provided code implementation in different languages as well.If you want to solve more questions on Linked List, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>which is curated by our expert mentors at PrepBytes,.<\/p>\n<h2>FAQs to move all occurrences of an element to end in a linked list<\/h2>\n<p><strong>1. What is the time complexity for inserting at the end of the linked list?<\/strong><br \/>\nIn a singly linked list, the time complexity for inserting and deleting an element from the list is O(n).<\/p>\n<p><strong>2. Are linked list dynamic?<\/strong><br \/>\nLinked List is a dynamic structure, which means the list can grow or shrink depending on the data making it more powerful and flexible than Arrays. Unlike Arrays, Linked List is not stored in a contiguous memory location<\/p>\n<p><strong>3. Is linked list static?<\/strong><br \/>\nLinked list are dynamic data structures while arrays are static data structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we will learn how to move all occurrences of an element to end in a linked list. As we all know Linked list is a linear data structure in which elements are linked using pointers and are not stored in contiguous memory locations. Let&#8217;s try to understand how to move all occurrences [&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-4232","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>Move all occurrences of an element to end in a linked list<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to move all occurrences of an element to end in a linked list. This blog explains how to move all occurrences of an element to end in a 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\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Move all occurrences of an element to end in a linked list\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to move all occurrences of an element to end in a linked list. This blog explains how to move all occurrences of an element to end in a linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-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-24T12:10:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-10T12:30:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.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=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Move all occurrences of an element to end in a linked list\",\"datePublished\":\"2021-08-24T12:10:04+00:00\",\"dateModified\":\"2022-11-10T12:30:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/\"},\"wordCount\":1424,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/\",\"name\":\"Move all occurrences of an element to end in a linked list\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png\",\"datePublished\":\"2021-08-24T12:10:04+00:00\",\"dateModified\":\"2022-11-10T12:30:44+00:00\",\"description\":\"Learn the most efficient way to move all occurrences of an element to end in a linked list. This blog explains how to move all occurrences of an element to end in a linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-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\":\"Move all occurrences of an element to end in a 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":"Move all occurrences of an element to end in a linked list","description":"Learn the most efficient way to move all occurrences of an element to end in a linked list. This blog explains how to move all occurrences of an element to end in a 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\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Move all occurrences of an element to end in a linked list","og_description":"Learn the most efficient way to move all occurrences of an element to end in a linked list. This blog explains how to move all occurrences of an element to end in a linked list.","og_url":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-24T12:10:04+00:00","article_modified_time":"2022-11-10T12:30:44+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Move all occurrences of an element to end in a linked list","datePublished":"2021-08-24T12:10:04+00:00","dateModified":"2022-11-10T12:30:44+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/"},"wordCount":1424,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/","name":"Move all occurrences of an element to end in a linked list","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png","datePublished":"2021-08-24T12:10:04+00:00","dateModified":"2022-11-10T12:30:44+00:00","description":"Learn the most efficient way to move all occurrences of an element to end in a linked list. This blog explains how to move all occurrences of an element to end in a linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916488102-82move-all-occureces_Artboard%201.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/move-all-occurrences-of-an-element-to-end-in-a-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":"Move all occurrences of an element to end in a 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\/4232","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=4232"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4232\/revisions"}],"predecessor-version":[{"id":10443,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4232\/revisions\/10443"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}