{"id":3792,"date":"2021-08-10T13:51:53","date_gmt":"2021-08-10T13:51:53","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3792"},"modified":"2022-11-14T11:45:11","modified_gmt":"2022-11-14T11:45:11","slug":"given-a-linked-list-move-the-last-element-to-the-front","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/","title":{"rendered":"Given a Linked List move the last element to the front"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png\" alt=\"\" \/><br \/>\nThis blog consists of step by step solving to move last element to front of a linked list. Linked list is the most important part of data structures. Having knowledge about data structures will increase the logic building and problem solving skills. Let\u2019s deep dive into our topic to move last element to first in linked list.<\/p>\n<\/p>\n<h2>How To Move Last Element To Front Of A Linked List<\/h2>\n<p>In this problem, we are given a linked list, and we have to move the last node of the linked list to the very first and make it head or root.<\/p>\n<p><strong>Input:<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/53_input-01.png\" alt=\"\" \/><\/p>\n<p><strong>Output (After moving the last element to the front of the linked list):<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-01.png\" alt=\"\" \/><\/p>\n<p><strong>Let\u2019s try to understand the problem by taking an example.<br \/>\nInitial Linked list<\/strong> = 7 \u2192 5 \u2192 3 \u2192 1<\/p>\n<ul>\n<li>We will start with traversing the linked list from the head until we have reached the last node of the linked list.  <\/li>\n<li>Once we have reached the last node of the linked list, we will make the previous node of the last node point to NULL and the next of the last node will point to the head node. <\/li>\n<li>Finally, we will make the node which we inserted at the front of the linked list the new head of the linked list.<\/li>\n<\/ul>\n<p>After moving the last node to the front of the linked list, our linked list will look like:<br \/>\n<strong>Updated Linked list<\/strong> = 1 \u2192 7 \u2192 5 \u2192 3.<\/p>\n<p>Well, this is a very basic problem, and furthermore we will continue with the approach to solve the problem.<\/p>\n<h2>Approach and Algorithm To Move Last Element To Front Of A Linked List<\/h2>\n<p>The most naive idea is to traverse the list till the last node or end of the LinkedList. Use two node pointers last and secLast &#8211; last used to store the address of the last node and the secLast is used to store the address of the second last node. After the end of the loop, do the following operations.<\/p>\n<ul>\n<li>We have to make the second last as last (<em>secLast-&gt;next = NULL<\/em>).<\/li>\n<li>Next step is to change next of last as head (_last-&gt;next = *head<em>ref<\/em>).<\/li>\n<li>The final step is to make last as the head (_*head<em>ref = last<\/em>).<\/li>\n<\/ul>\n<h3>Dry Run To Move Last Element To Front Of A Linked List<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/final-3-01.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation To Move Last Element To Front Of A Linked List<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3784 {\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_3784 .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_3784 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3784 .wpsm_nav-tabs > li.active > a, #tab_container_3784 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3784 .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_3784 .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_3784 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3784 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3784 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3784 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3784 .wpsm_nav-tabs > li > a:hover , #tab_container_3784 .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_3784 .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_3784 .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_3784 .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_3784 .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_3784 .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_3784 .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_3784 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3784 .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_3784 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3784 .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_3784 .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_3784\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3784\">\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_3784_1\" aria-controls=\"tabs_desc_3784_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_3784_2\" aria-controls=\"tabs_desc_3784_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_3784_3\" aria-controls=\"tabs_desc_3784_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_3784_4\" aria-controls=\"tabs_desc_3784_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_3784\">\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_3784_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\n\/* A linked list node *\/\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node *next;\r\n};\r\n \r\n\/* We are using a double pointer head_ref here because we change\r\n   head of the linked list inside this function.*\/\r\nvoid moveToFront(struct Node **head_ref)\r\n{\r\n    \/* If linked list is empty, or it contains only one node,\r\n      then nothing needs to be done, simply return *\/\r\n    if (*head_ref == NULL || (*head_ref)-&gt;next == NULL)\r\n        return;\r\n \r\n    \/* Initialize second last and last pointers *\/\r\n    struct Node *secLast = NULL;\r\n    struct Node *last = *head_ref;\r\n \r\n    \/*After this loop secLast contains address of second last\r\n    node and last contains address of last node in Linked List *\/\r\n    while (last-&gt;next != NULL)\r\n    {\r\n        secLast = last;\r\n        last = last-&gt;next;\r\n    }\r\n \r\n    \/* Set the next of second last as NULL *\/\r\n    secLast-&gt;next = NULL;\r\n \r\n    \/* Set next of last as head node *\/\r\n    last-&gt;next = *head_ref;\r\n \r\n    \/* Change the head pointer to point to last node now *\/\r\n    *head_ref = last;\r\n}\r\n \r\n\/* UTILITY FUNCTIONS *\/\r\n\/* Function to add a node at the beginning of Linked List *\/\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    \/* allocate node *\/\r\n    struct Node* new_node =\r\n        (struct Node*) malloc(sizeof(struct Node));\r\n \r\n    \/* put in the data  *\/\r\n    new_node-&gt;data  = new_data;\r\n \r\n    \/* link the old list off the new node *\/\r\n    new_node-&gt;next = (*head_ref);\r\n \r\n    \/* move the head to point to the new node *\/\r\n    (*head_ref)    = new_node;\r\n}\r\n \r\n \r\n\/* Function to print nodes in a given linked list *\/\r\nvoid printList(struct Node *node)\r\n{\r\n    while(node != NULL)\r\n    {\r\n        printf(&quot;%d &quot;, node-&gt;data);\r\n        node = node-&gt;next;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above function *\/\r\nint main()\r\n{\r\n    struct Node *start = NULL;\r\n \r\n    \/* The constructed linked list is:\r\n     1-&gt;2-&gt;3-&gt;4-&gt;5 *\/\r\n    push(&amp;start, 5);\r\n    push(&amp;start, 4);\r\n    push(&amp;start, 3);\r\n    push(&amp;start, 2);\r\n    push(&amp;start, 1);\r\n \r\n    printf(&quot;&#92;n Linked list before moving last to front&#92;n&quot;);\r\n    printList(start);\r\n \r\n    moveToFront(&amp;start);\r\n \r\n    printf(&quot;&#92;n Linked list after removing last to front&#92;n&quot;);\r\n    printList(start);\r\n \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_3784_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\nclass Node\r\n{\r\n    public:\r\n    int data;\r\n    Node *next;\r\n};\r\nvoid moveToFront(Node **head_ref)\r\n{\r\n    if (*head_ref == NULL || *head_ref-&gt;next == NULL)\r\n        return;\r\n    Node *secLast = NULL;\r\n    Node *last = *head_ref;\r\n    while (last-&gt;next != NULL)\r\n    {\r\n        secLast = last;\r\n        last = last-&gt;next;\r\n    }\r\n    secLast-&gt;next = NULL;\r\n    last-&gt;next = *head_ref;\r\n    *head_ref = last;\r\n}\r\nvoid push(Node** head_ref, int new_data)\r\n{\r\n    Node* new_node = new Node();\r\n    new_node-&gt;data = new_data;\r\n    new_node-&gt;next = *head_ref;\r\n    *head_ref = new_node;\r\n}\r\nvoid printList(Node *node)\r\n{\r\n    while(node != NULL)\r\n    {\r\n        cout &lt;&lt; node-&gt;data &lt;&lt; &quot; &quot;;\r\n        node = node-&gt;next;\r\n    }\r\n}\r\nint main()\r\n{\r\n    Node *start = NULL;\r\n    push(&amp;start, 1);\r\n    push(&amp;start, 3);\r\n    push(&amp;start, 5);\r\n    push(&amp;start, 7);\r\n    moveToFront(&amp;start);\r\n    printList(start);\r\n    return 0;\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_3784_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 MoveLast\r\n{\r\n    Node head; \r\n\r\n    class Node\r\n    {\r\n        int data;\r\n        Node next;\r\n        Node(int d) {data = d; next = null; }\r\n    }\r\n\r\n    void moveToFront()\r\n    {\r\n        if(head == null || head.next == null)\r\n            return;\r\n\r\n        \/* Initialize second last and last pointers *\/\r\n        Node secLast = null;\r\n        Node last = head;\r\n\r\n        while (last.next != null)\r\n        {\r\n        secLast = last;\r\n        last = last.next;\r\n        }\r\n\r\n        secLast.next = null;\r\n        last.next = head;\r\n        head = last;\r\n    }                \r\n    void push(int new_data)\r\n    {\r\n        Node new_node = new Node(new_data);\r\n        new_node.next = head;\r\n        head = new_node;\r\n    }\r\n    void printList()\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        System.out.println();\r\n    }\r\n    public static void main(String args[])\r\n    {\r\n        MoveLast llist = new MoveLast();\r\n    \r\n        llist.push(5);\r\n        llist.push(4);\r\n        llist.push(3);\r\n        llist.push(2);\r\n        llist.push(1);\r\n        \r\n        System.out.println(&quot;Linked List before moving last to front &quot;);\r\n        llist.printList();\r\n        \r\n        llist.moveToFront();\r\n        \r\n        System.out.println(&quot;Linked List after moving last to front &quot;);\r\n        llist.printList();\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_3784_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    def __init__(self, data):\r\n        self.data = data\r\n        self.next = None\r\n\r\nclass LinkedList:\r\n    def __init__(self):\r\n        self.head = None\r\n\r\n    def push(self, data):\r\n        new_node = Node(data)\r\n        new_node.next = self.head\r\n        self.head = new_node\r\n        \r\n    def printList(self):\r\n        tmp = self.head\r\n        while tmp is not None:\r\n            print(tmp.data, end=&quot; &quot;)\r\n            tmp = tmp.next\r\n        print()\r\n\r\n    def moveToFront(self):\r\n        tmp = self.head\r\n        sec_last = None\r\n\r\n        if not tmp or not tmp.next:\r\n            return\r\n\r\n        while tmp and tmp.next :\r\n            sec_last = tmp\r\n            tmp = tmp.next\r\n\r\n        sec_last.next = None\r\n\r\n        tmp.next = self.head\r\n        self.head = tmp\r\n\r\nif __name__ == '__main__':\r\n    llist = LinkedList()\r\n    \r\n    llist.push(1)\r\n    llist.push(3)\r\n    llist.push(5)\r\n    llist.push(7)\r\n    llist.moveToFront()\r\n    llist.printList()\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_3784 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_3784 a\"),jQuery(\"#tab-content_3784\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<p>1 7 5 3<\/p>\n<p><strong>Time Complexity To Move Last Element To Front Of A Linked List:<\/strong> O(n), where n is the number of nodes in the given LinkedList.<\/p>\n<p><strong>Space Complexity To Move Last Element To Front Of A Linked List:<\/strong> O(1), constant space complexity, as no extra space is used.<\/p>\n<p>This blog taught us an efficient approach of solving the problem \u201cMove last element to first in linked list\u201d with best illustration of approach, algorithm, and dry run to move last element to first in linked list. Linked list is a topic which should be practiced for placing in tech companies like Amazon, Adobe, Google, Flipkart etc. If you want to practice more questions on linked lists, feel free to solve them at<a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<h2>FAQ<\/h2>\n<p><ol><strong><\/p>\n<li>What is the linked list?<\/li>\n<p><\/strong>A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array.<br \/>\n<strong><\/p>\n<li>What is the node in the linked list?<\/li>\n<p><\/strong>A node is a collection of two sub-elements or parts. A data part that stores the element and a next part that stores the link to the next node.<br \/>\n<strong><\/p>\n<li>Which companies asked questions about linked lists in their interviews?<\/li>\n<p><\/strong>Companies like Josh Technologies, Samsung, Uber, Indiamart and Amazon asked questions related to linked lists in their interviews.<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This blog consists of step by step solving to move last element to front of a linked list. Linked list is the most important part of data structures. Having knowledge about data structures will increase the logic building and problem solving skills. Let\u2019s deep dive into our topic to move last element to first in [&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-3792","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>Given a Linked List move last element to front of a given Linked List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn how to move the last element of the linked list to the front in the most efficient way. This blog explains how you can move the last element of the linked list to the front of the 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\/given-a-linked-list-move-the-last-element-to-the-front\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Given a Linked List move last element to front of a given Linked List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn how to move the last element of the linked list to the front in the most efficient way. This blog explains how you can move the last element of the linked list to the front of the list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/\" \/>\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-10T13:51:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-14T11:45:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Given a Linked List move the last element to the front\",\"datePublished\":\"2021-08-10T13:51:53+00:00\",\"dateModified\":\"2022-11-14T11:45:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/\"},\"wordCount\":636,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/\",\"name\":\"Given a Linked List move last element to front of a given Linked List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png\",\"datePublished\":\"2021-08-10T13:51:53+00:00\",\"dateModified\":\"2022-11-14T11:45:11+00:00\",\"description\":\"Learn how to move the last element of the linked list to the front in the most efficient way. This blog explains how you can move the last element of the linked list to the front of the list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#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\":\"Given a Linked List move the last element to the front\"}]},{\"@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":"Given a Linked List move last element to front of a given Linked List | Linked List | Prepbytes","description":"Learn how to move the last element of the linked list to the front in the most efficient way. This blog explains how you can move the last element of the linked list to the front of the 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\/given-a-linked-list-move-the-last-element-to-the-front\/","og_locale":"en_US","og_type":"article","og_title":"Given a Linked List move last element to front of a given Linked List | Linked List | Prepbytes","og_description":"Learn how to move the last element of the linked list to the front in the most efficient way. This blog explains how you can move the last element of the linked list to the front of the list.","og_url":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-10T13:51:53+00:00","article_modified_time":"2022-11-14T11:45:11+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Given a Linked List move the last element to the front","datePublished":"2021-08-10T13:51:53+00:00","dateModified":"2022-11-14T11:45:11+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/"},"wordCount":636,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/","url":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/","name":"Given a Linked List move last element to front of a given Linked List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png","datePublished":"2021-08-10T13:51:53+00:00","dateModified":"2022-11-14T11:45:11+00:00","description":"Learn how to move the last element of the linked list to the front in the most efficient way. This blog explains how you can move the last element of the linked list to the front of the list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916573928-83move-last-element_Artboard%202.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/given-a-linked-list-move-the-last-element-to-the-front\/#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":"Given a Linked List move the last element to the front"}]},{"@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\/3792","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=3792"}],"version-history":[{"count":6,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3792\/revisions"}],"predecessor-version":[{"id":10468,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3792\/revisions\/10468"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}