{"id":1823,"date":"2020-06-11T18:57:13","date_gmt":"2020-06-11T18:57:13","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=1823"},"modified":"2022-11-22T09:49:05","modified_gmt":"2022-11-22T09:49:05","slug":"reorder-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/reorder-list\/","title":{"rendered":"REORDER LIST"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png\" alt=\"\" \/><\/p>\n<p>In this article, we will discuss different approaches to reorder linked list. Reordering a linked list will always clear the topic like a linked list. And Having knowledge about linked list will make your data structures strong.<\/p>\n<h2>How to Reorder List<\/h2>\n<blockquote>\n<p>Given a singly linked list <code>L:L0\u2192L1\u2192\u2026\u2192Ln\u22121\u2192Ln<\/code>,<br \/>\nreorder it to <code>:L0\u2192Ln\u2192L1\u2192Ln\u22121\u2192L2\u2192Ln\u22122\u2192\u2026<\/code><br \/>\nYou must do this in-place without altering the nodes\u2019 values.<br \/>\nExpected Time Complexity O<code>(<\/code>n<code>)<\/code>.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/linked-list\/REORDERLIST\" title=\"Go to mycode.prepbytes.com\" target=\"_blank\" rel=\"noopener noreferrer\"><u><strong><\/strong><\/u><\/a><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code> Given 1-&gt;2-&gt;3-&gt;4,\n  reorder it to 1-&gt;4-&gt;2-&gt;3.<\/code><\/pre>\n<h2>Approaches to Reorder List<\/h2>\n<h3>Approach 1(Brute force) To Reorder The Given Linked List:<\/h3>\n<blockquote>\n<p>1) Initialize the current node as head.<\/p>\n<p>2) While next of current node is not null,find the last node, remove it from the end and insert it as next of the current node.<\/p>\n<p>3) Move current to next of current.<\/p>\n<p><strong>TIME COMPLEXITY:<\/strong> O<code>(<\/code>n*n<code>)<\/code><\/p>\n<\/blockquote>\n<h3>Approach 2 (Efficient solution) To Reorder The Given Linked List:<\/h3>\n<blockquote>\n<p>This approach uses two pointers(tortoise and hare method) to find the middle of linked list.Reverse the second half and alternately merge the first and second halves.<\/p>\n<\/blockquote>\n<h3>Approach 3 (Deque) To Reorder The Given Linked List:<\/h3>\n<blockquote>\n<p>1) Create an empty deque.<\/p>\n<p>2) Insert all elements from the linked list to the deque.<\/p>\n<p>3) Insert the element back to the linked list from deque in alternate fashion i.e first then last and so on.<\/p>\n<\/blockquote>\n<h2>Code implementations<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_1826 {\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_1826 .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_1826 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_1826 .wpsm_nav-tabs > li.active > a, #tab_container_1826 .wpsm_nav-tabs > li.active > a:hover, #tab_container_1826 .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_1826 .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_1826 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_1826 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_1826 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_1826 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_1826 .wpsm_nav-tabs > li > a:hover , #tab_container_1826 .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_1826 .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_1826 .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_1826 .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_1826 .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_1826 .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_1826 .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_1826 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1826 .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_1826 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1826 .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_1826 .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_1826\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_1826\">\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_1826_1\" aria-controls=\"tabs_desc_1826_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_1826_2\" aria-controls=\"tabs_desc_1826_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_1826_3\" aria-controls=\"tabs_desc_1826_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_1826_4\" aria-controls=\"tabs_desc_1826_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_1826\">\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_1826_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\nstruct Node { \r\n    int data; \r\n    struct Node* next; \r\n}; \r\n\r\n\/\/ Function to create newNode in a linkedlist \r\nstruct Node* newNode(int key) \r\n{ \r\n    struct Node* temp;\r\n    temp=(struct Node *)malloc(sizeof(struct Node));\r\n    temp-&gt;data = key; \r\n    temp-&gt;next = NULL; \r\n    return temp; \r\n} \r\n\r\n\/\/ Function to reverse the linked list \r\nvoid reverselist(struct Node** head) \r\n{ \r\n    \/\/ Initialize prev and current pointers \r\n    struct Node *prev = NULL, *curr = *head, *next; \r\n\r\n    while (curr) { \r\n        next = curr-&gt;next; \r\n        curr-&gt;next = prev; \r\n        prev = curr; \r\n        curr = next; \r\n    } \r\n\r\n    *head = prev; \r\n} \r\n\r\n\/\/ Function to print the linked list \r\nvoid printlist(struct Node* head) \r\n{ \r\n    while (head != NULL) { \r\n        printf(&quot;%d &quot;,head-&gt;data); \r\n        if (head-&gt;next) \r\n            printf(&quot; &quot;); \r\n        head = head-&gt;next; \r\n    } \r\n    printf(&quot;&#92;n&quot;); \r\n} \r\n\r\n    void rearrange(struct Node** head) \r\n    { \r\n    \/\/ 1) Find the middle point using tortoise and hare method \r\n    struct Node *slow = *head, *fast = slow-&gt;next; \r\n    while (fast &amp;&amp; fast-&gt;next) { \r\n        slow = slow-&gt;next; \r\n        fast = fast-&gt;next-&gt;next; \r\n    } \r\n    struct Node* head1 = *head; \r\n    struct Node* head2 = slow-&gt;next; \r\n    slow-&gt;next = NULL; \r\n\r\n    reverselist(&amp;head2); \r\n\r\n    *head = newNode(0); \r\n    struct Node* curr = *head; \r\n    while (head1 || head2) { \r\n        if (head1) { \r\n            curr-&gt;next = head1; \r\n            curr = curr-&gt;next; \r\n            head1 = head1-&gt;next; \r\n        } \r\n        if (head2) { \r\n            curr-&gt;next = head2; \r\n            curr = curr-&gt;next; \r\n            head2 = head2-&gt;next; \r\n        } \r\n    } \r\n\r\n    \/\/ Assign the head of the new list to head pointer \r\n    *head = (*head)-&gt;next; \r\n    } \r\n     int main() \r\n    {   int n;scanf(&quot;%d&quot;,&amp;n);\r\n   int x;scanf(&quot;%d&quot;,&amp;x);\r\n\r\n    struct Node* head = newNode(x); \r\n    struct Node* headlist=head;\r\n    for(int i=1;i&lt;n;i++)\r\n    {\r\n      scanf(&quot;%d&quot;,&amp;x);\r\n      head-&gt;next=newNode(x);\r\n      head=head-&gt;next;\r\n    }\r\n    \/\/head-&gt;next=NULL;\r\n    printf(&quot;%d &quot;,headlist-&gt;data);\r\n    rearrange(&amp;headlist); \/\/ Modify the list \r\n    printlist(head); \/\/ Print modified list \r\n    return 0; \r\n    } \r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1826_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\nstruct Node { \r\n    int data; \r\n    struct Node* next; \r\n}; \r\n\r\n\/\/ Function to create newNode in a linkedlist \r\nNode* newNode(int key) \r\n{ \r\n    Node* temp = new Node; \r\n    temp-&gt;data = key; \r\n    temp-&gt;next = NULL; \r\n    return temp; \r\n} \r\n\r\n\/\/ Function to reverse the linked list \r\nvoid reverselist(Node** head) \r\n{ \r\n    \/\/ Initialize prev and current pointers \r\n    Node *prev = NULL, *curr = *head, *next; \r\n\r\n    while (curr) { \r\n        next = curr-&gt;next; \r\n        curr-&gt;next = prev; \r\n        prev = curr; \r\n        curr = next; \r\n    } \r\n\r\n    *head = prev; \r\n} \r\n\r\n\/\/ Function to print the linked list \r\nvoid printlist(Node* head) \r\n{ \r\n    while (head != NULL) { \r\n        cout &lt;&lt; head-&gt;data &lt;&lt; &quot; &quot;; \r\n        if (head-&gt;next) \r\n            cout &lt;&lt; &quot; &quot;; \r\n        head = head-&gt;next; \r\n    } \r\n    cout &lt;&lt; endl; \r\n} \r\n\r\n    void rearrange(Node** head) \r\n    { \r\n    \/\/ 1) Find the middle point using tortoise and hare method \r\n    Node *slow = *head, *fast = slow-&gt;next; \r\n    while (fast &amp;&amp; fast-&gt;next) { \r\n        slow = slow-&gt;next; \r\n        fast = fast-&gt;next-&gt;next; \r\n    } \r\n    Node* head1 = *head; \r\n    Node* head2 = slow-&gt;next; \r\n    slow-&gt;next = NULL; \r\n\r\n    reverselist(&amp;head2); \r\n\r\n    *head = newNode(0); \r\n    Node* curr = *head; \r\n    while (head1 || head2) { \r\n        if (head1) { \r\n            curr-&gt;next = head1; \r\n            curr = curr-&gt;next; \r\n            head1 = head1-&gt;next; \r\n        } \r\n        if (head2) { \r\n            curr-&gt;next = head2; \r\n            curr = curr-&gt;next; \r\n            head2 = head2-&gt;next; \r\n        } \r\n    } \r\n\r\n    \/\/ Assign the head of the new list to head pointer \r\n    *head = (*head)-&gt;next; \r\n    } \r\n     int main() \r\n    {   int n;cin&gt;&gt;n;\r\n   int x;cin&gt;&gt;x;\r\n\r\n    Node* head = newNode(x); \r\n    Node* headlist=head;\r\n    for(int i=1;i&lt;n;i++)\r\n    {\r\n      cin&gt;&gt;x;\r\n      head-&gt;next=newNode(x);\r\n      head=head-&gt;next;\r\n    }\r\n    \/\/head-&gt;next=NULL;\r\n    cout&lt;&lt;headlist-&gt;data&lt;&lt;&quot; &quot;;\r\n    rearrange(&amp;headlist); \/\/ Modify the list \r\n    printlist(head); \/\/ Print modified list \r\n    return 0; \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_1826_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*; \r\n    import java.lang.*; \r\n    import java.io.*; \r\n\r\n    class prepbytes\r\n    { \r\n    static class Node  \r\n    {  \r\n        int data;  \r\n        Node next; \r\n        Node(int data) \r\n        { \r\n            this.data = data; \r\n            next = null; \r\n        } \r\n    } \r\n    static void printList(Node head)  \r\n    {  \r\n        Node temp = head;  \r\n        while (temp != null)  \r\n        {  \r\n            System.out.print(temp.data + &quot; &quot;);  \r\n            temp = temp.next;  \r\n        }  \r\n    } \r\n    static void arrange(Node head) \r\n    { \r\n        Deque&lt;Integer&gt; deque = new ArrayDeque&lt;&gt;(); \r\n        Node temp = head; \r\n        while(temp != null) \r\n        { \r\n            deque.addLast(temp.data); \r\n            temp = temp.next; \r\n        } \r\n        temp = head; \r\n        int i = 0; \r\n        while(!deque.isEmpty()) \r\n        { \r\n            if(i % 2 == 0) \r\n            { \r\n                temp.data = deque.removeFirst(); \r\n            } \r\n            else\r\n            { \r\n                temp.data = deque.removeLast(); \r\n            } \r\n            i++; \r\n            temp = temp.next; \r\n        } \r\n    } \r\n    public static void main (String[] args) \r\n    { Scanner sc = new Scanner(System.in);\r\n        int n= sc.nextInt();\r\n         int x = sc.nextInt();\r\n        Node head = new Node(x);\r\n        Node headlist=head;\r\n            for(int i=1;i&lt;n;i++)\r\n            {\r\n                x = sc.nextInt();\r\n                head.next=new Node(x);\r\n                head=head.next;\r\n            }\r\n\r\n        arrange(headlist);   \r\n        printList(headlist); \r\n    } \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_1826_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\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\r\n\tdef __init__(self, d):\r\n\t\tself.data = d\r\n\t\tself.next = None\r\n\t\t\r\ndef printlist(node):\r\n\tif(node == None):\r\n\t\treturn\r\n\twhile(node != None):\r\n\t\tprint(node.data,&quot; -&gt; &quot;, end = &quot;&quot;)\r\n\t\tnode = node.next\r\n\r\ndef reverselist(node):\r\n\tprev = None\r\n\tcurr = node\r\n\tnext=None\r\n\twhile (curr != None):\r\n\t\tnext = curr.next\r\n\t\tcurr.next = prev\r\n\t\tprev = curr\r\n\t\tcurr = next\r\n\tnode = prev\r\n\treturn node\r\n\r\ndef rearrange(node):\r\n\r\n\tslow = node\r\n\tfast = slow.next\r\n\twhile (fast != None and fast.next != None):\r\n\t\tslow = slow.next\r\n\t\tfast = fast.next.next\r\n\t\r\n\tnode1 = node\r\n\tnode2 = slow.next\r\n\tslow.next = None\r\n\t\r\n\tnode2 = reverselist(node2)\r\n\t\r\n\tnode = Node(0) \r\n\t\r\n\tcurr = node\r\n\t\r\n\twhile (node1 != None or node2 != None):\r\n\t\t\r\n\t\tif (node1 != None):\r\n\t\t\tcurr.next = node1\r\n\t\t\tcurr = curr.next\r\n\t\t\tnode1 = node1.next\r\n\t\t\r\n\t\tif(node2 != None):\r\n\t\t\tcurr.next = node2\r\n\t\t\tcurr = curr.next\r\n\t\t\tnode2 = node2.next\r\n\t\r\n\tnode = node.next\r\n\r\nhead = None\r\nhead = Node(1)\r\nhead.next = Node(2)\r\nhead.next.next = Node(3)\r\nhead.next.next.next = Node(4)\r\nhead.next.next.next.next = Node(5)\r\n\r\nprintlist(head) \r\nrearrange(head) \r\nprint()\r\nprintlist(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_1826 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_1826 a\"),jQuery(\"#tab-content_1826\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p>In this article, we have discussed multiple approaches to reorder the given linked list. Practicing &#8220;reorder the given linked list&#8221; will help you to understand linked lists in detail. To practice more problems on Linked List you can check out <a href=\"#\"><\/a>.<\/p>\n<h2>FAQ<\/h2>\n<p><strong>1. What defines an ordered list?<\/strong><br \/>\nAn ordered list defines a list of items in which the order of the items matters.<\/p>\n<p><strong>2. How do you swap data between two nodes in a linked list?<\/strong><br \/>\nThe idea is to first search x and y in the given linked list. If any of them is not present, then return. While searching for x and y, keep track of current and previous pointers. First change next of previous pointers, then change next of current pointers.<\/p>\n<p><strong>3. What is a linked list in data structure?<\/strong><br \/>\nA linked list is the most sought-after data structure when it comes to handling dynamic data elements. A linked list consists of a data element known as a node. And each node consists of two fields: one field has data, and in the second field, the node has an address that keeps a reference to the next node.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will discuss different approaches to reorder linked list. Reordering a linked list will always clear the topic like a linked list. And Having knowledge about linked list will make your data structures strong. How to Reorder List Given a singly linked list L:L0\u2192L1\u2192\u2026\u2192Ln\u22121\u2192Ln, reorder it to :L0\u2192Ln\u2192L1\u2192Ln\u22121\u2192L2\u2192Ln\u22122\u2192\u2026 You must do this [&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-1823","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>Reorder The Given Linked List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Initialize the current node as head. While next of current node is not null,find the last node, remove it from the end and insert it as next of the current node.\" \/>\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\/reorder-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reorder The Given Linked List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Initialize the current node as head. While next of current node is not null,find the last node, remove it from the end and insert it as next of the current node.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/reorder-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=\"2020-06-11T18:57:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-22T09:49:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"REORDER LIST\",\"datePublished\":\"2020-06-11T18:57:13+00:00\",\"dateModified\":\"2022-11-22T09:49:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/\"},\"wordCount\":394,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reorder-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/\",\"name\":\"Reorder The Given Linked List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png\",\"datePublished\":\"2020-06-11T18:57:13+00:00\",\"dateModified\":\"2022-11-22T09:49:05+00:00\",\"description\":\"Initialize the current node as head. While next of current node is not null,find the last node, remove it from the end and insert it as next of the current node.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reorder-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reorder-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\":\"REORDER 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":"Reorder The Given Linked List | Linked List | Prepbytes","description":"Initialize the current node as head. While next of current node is not null,find the last node, remove it from the end and insert it as next of the current node.","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\/reorder-list\/","og_locale":"en_US","og_type":"article","og_title":"Reorder The Given Linked List | Linked List | Prepbytes","og_description":"Initialize the current node as head. While next of current node is not null,find the last node, remove it from the end and insert it as next of the current node.","og_url":"https:\/\/prepbytes.com\/blog\/reorder-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-06-11T18:57:13+00:00","article_modified_time":"2022-11-22T09:49:05+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"REORDER LIST","datePublished":"2020-06-11T18:57:13+00:00","dateModified":"2022-11-22T09:49:05+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/"},"wordCount":394,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/reorder-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/","url":"https:\/\/prepbytes.com\/blog\/reorder-list\/","name":"Reorder The Given Linked List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png","datePublished":"2020-06-11T18:57:13+00:00","dateModified":"2022-11-22T09:49:05+00:00","description":"Initialize the current node as head. While next of current node is not null,find the last node, remove it from the end and insert it as next of the current node.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/reorder-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/reorder-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645182004679-Article_473.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/reorder-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":"REORDER 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\/1823","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=1823"}],"version-history":[{"count":15,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1823\/revisions"}],"predecessor-version":[{"id":10683,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1823\/revisions\/10683"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=1823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=1823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=1823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}