{"id":27620,"date":"2018-04-09T20:10:08","date_gmt":"2018-04-09T14:40:08","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27620"},"modified":"2018-09-16T13:29:50","modified_gmt":"2018-09-16T07:59:50","slug":"java-algorithm-move-last-element-front-given-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-move-last-element-front-given-linked-list\/","title":{"rendered":"Java Algorithm &#8211; Move last element to front of a given Linked List"},"content":{"rendered":"<p>Write a C function that moves last element to front in a given Singly Linked List. For example, if the given Linked List is 1-&gt;2-&gt;3-&gt;4-&gt;5, then the function should change the list to 5-&gt;1-&gt;2-&gt;3-&gt;4.<span id=\"more-6850\"><\/span><\/p>\n<p>Algorithm:<br \/>\nTraverse the list till last node. Use two pointers: one to store the address of last node and other for address of second last node. After the end of loop do following operations.<br \/>\ni) Make second last as last (secLast-&gt;next = NULL).<br \/>\nii) Set next of last as head (last-&gt;next = *head_ref).<br \/>\niii) Make last as head ( *head_ref = last)<\/p>\n<p><strong>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/* Java Program to move last element to front in a given linked list *\/<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of list<br\/>  <br\/>    \/* Linked list Node*\/<br\/>    class Node<br\/>    {<br\/>        int data;<br\/>        Node next;<br\/>        Node(int d) {data = d; next = null; }<br\/>    }<br\/> <br\/>    void moveToFront()<br\/>    {<br\/>        \/* If linked list is empty or it contains only<br\/>           one node then simply return. *\/<br\/>           if(head == null || head.next == null) <br\/>              return;<br\/> <br\/>        \/* Initialize second last and last pointers *\/<br\/>        Node secLast = null;<br\/>        Node last = head;<br\/> <br\/>        \/* After this loop secLast contains address of <br\/>           second last  node and last contains address of <br\/>           last node in Linked List *\/<br\/>        while (last.next != null)  <br\/>        {<br\/>           secLast = last;<br\/>           last = last.next; <br\/>        }<br\/> <br\/>        \/* Set the next of second last as null *\/<br\/>        secLast.next = null;<br\/> <br\/>        \/* Set the next of last as head *\/<br\/>        last.next = head;<br\/> <br\/>        \/* Change head to point to last node. *\/<br\/>        head = last;<br\/>    }                 <br\/> <br\/>                     <br\/>    \/* Utility functions *\/<br\/> <br\/>    \/* Inserts a new Node at front of the list. *\/<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/* 1 &amp; 2: Allocate the Node &amp;<br\/>                  Put in the data*\/<br\/>        Node new_node = new Node(new_data);<br\/>  <br\/>        \/* 3. Make next of new Node as head *\/<br\/>        new_node.next = head;<br\/>  <br\/>        \/* 4. Move the head to point to new Node *\/<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    \/* Function to print linked list *\/<br\/>    void printList()<br\/>    {<br\/>        Node temp = head;<br\/>        while(temp != null)<br\/>        {<br\/>           System.out.print(temp.data+&quot; &quot;);<br\/>           temp = temp.next;<br\/>        }  <br\/>        System.out.println();<br\/>    }<br\/> <br\/>     \/* Drier program to test above functions *\/<br\/>    public static void main(String args[])<br\/>    {<br\/>        LinkedList llist = new LinkedList();<br\/>        \/* Constructed Linked List is 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;null *\/<br\/>        llist.push(5);<br\/>        llist.push(4);<br\/>        llist.push(3);<br\/>        llist.push(2);<br\/>        llist.push(1);<br\/>         <br\/>        System.out.println(&quot;Linked List before moving last to front &quot;);<br\/>        llist.printList();<br\/>         <br\/>        llist.moveToFront();<br\/>         <br\/>        System.out.println(&quot;Linked List after moving last to front &quot;);<br\/>        llist.printList();<br\/>    }<br\/>} <br\/>\/* This code is contributed by Rajat Mishra *\/<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre> Linked list before moving last to front \r\n1 2 3 4 5 \r\n Linked list after removing last to front \r\n5 1 2 3<\/pre>\n<p>Time Complexity: O(n) where n is the number of nodes in the given Linked List.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Algorithm &#8211; Move last element to front of a given Linked List &#8211; Linked List &#8211; Write a C function that moves last element to front in a given Singly<\/p>\n","protected":false},"author":1,"featured_media":31286,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81917,81924,81919,81920,81922,81925,81918,81586,81923,81921],"class_list":["post-27620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-java-linked-list-move-element-to-front","tag-java-list-move-element-to-another-position","tag-move-last-element-to-front-of-a-given-linked-list-java","tag-move-node-from-one-linked-list-to-another","tag-move-to-front-java","tag-sorted-vector","tag-swap-every-two-nodes-in-a-linked-list","tag-swapping-two-nodes-in-a-singly-linked-list","tag-unsorted-linked-list","tag-write-a-method-to-add-two-polynomial-equations-using-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27620"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27620\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31286"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}