{"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->2->3->4->5, then the function should change the list to 5->1->2->3->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->next = NULL).<br \/>\nii) Set next of last as head (last->next = *head_ref).<br \/>\niii) Make last as head ( *head_ref = last)<\/p>\n<p><strong>Java Programming:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F*%20Java%20Program%20to%20move%20last%20element%20to%20front%20in%20a%20given%20linked%20list%20*%2F%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%20%2F%2F%20head%20of%20list%0A%20%20%0A%20%20%20%20%2F*%20Linked%20list%20Node*%2F%0A%20%20%20%20class%20Node%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%20%20%20%20%20%20%20Node(int%20d)%20%7Bdata%20%3D%20d%3B%20next%20%3D%20null%3B%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20void%20moveToFront()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20If%20linked%20list%20is%20empty%20or%20it%20contains%20only%0A%20%20%20%20%20%20%20%20%20%20%20one%20node%20then%20simply%20return.%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20if(head%20%3D%3D%20null%20%7C%7C%20head.next%20%3D%3D%20null)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Initialize%20second%20last%20and%20last%20pointers%20*%2F%0A%20%20%20%20%20%20%20%20Node%20secLast%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20Node%20last%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20After%20this%20loop%20secLast%20contains%20address%20of%20%0A%20%20%20%20%20%20%20%20%20%20%20second%20last%20%20node%20and%20last%20contains%20address%20of%20%0A%20%20%20%20%20%20%20%20%20%20%20last%20node%20in%20Linked%20List%20*%2F%0A%20%20%20%20%20%20%20%20while%20(last.next%20!%3D%20null)%20%20%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20secLast%20%3D%20last%3B%0A%20%20%20%20%20%20%20%20%20%20%20last%20%3D%20last.next%3B%20%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Set%20the%20next%20of%20second%20last%20as%20null%20*%2F%0A%20%20%20%20%20%20%20%20secLast.next%20%3D%20null%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Set%20the%20next%20of%20last%20as%20head%20*%2F%0A%20%20%20%20%20%20%20%20last.next%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Change%20head%20to%20point%20to%20last%20node.%20*%2F%0A%20%20%20%20%20%20%20%20head%20%3D%20last%3B%0A%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%2F*%20Utility%20functions%20*%2F%0A%20%0A%20%20%20%20%2F*%20Inserts%20a%20new%20Node%20at%20front%20of%20the%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Put%20in%20the%20data*%2F%0A%20%20%20%20%20%20%20%20Node%20new_node%20%3D%20new%20Node(new_data)%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20Node%20as%20head%20*%2F%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20head%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%2F*%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%20*%2F%0A%20%20%20%20%20%20%20%20head%20%3D%20new_node%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20print%20linked%20list%20*%2F%0A%20%20%20%20void%20printList()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while(temp%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20System.out.print(temp.data%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%3B%0A%20%20%20%20%20%20%20%20%7D%20%20%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%2F*%20Drier%20program%20to%20test%20above%20functions%20*%2F%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20%2F*%20Constructed%20Linked%20List%20is%201-%3E2-%3E3-%3E4-%3E5-%3Enull%20*%2F%0A%20%20%20%20%20%20%20%20llist.push(5)%3B%0A%20%20%20%20%20%20%20%20llist.push(4)%3B%0A%20%20%20%20%20%20%20%20llist.push(3)%3B%0A%20%20%20%20%20%20%20%20llist.push(2)%3B%0A%20%20%20%20%20%20%20%20llist.push(1)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20List%20before%20moving%20last%20to%20front%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20llist.moveToFront()%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20List%20after%20moving%20last%20to%20front%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%20%20%20%7D%0A%7D%20%0A%2F*%20This%20code%20is%20contributed%20by%20Rajat%20Mishra%20*%2F%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}