Java Program For Moving Last Element To Front Of A Given Linked List Last Updated : 03 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Write a function that moves the last element to the 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. Algorithm: Traverse the list till the last node. Use two pointers: one to store the address of the last node and the other for the address of the second last node. After the end of the loop do the following operations. Make second last as last (secLast->next = NULL).Set next of last as head (last->next = *head_ref).Make last as head ( *head_ref = last). Java /* Java Program to move last element to front in a given linked list */ class LinkedList { // Head of list Node head; // Linked list Node class Node { int data; Node next; Node(int d) { data = d; next = null; } } void moveToFront() { /* If linked list is empty or it contains only one node then simply return. */ if(head == null || head.next == null) return; /* Initialize second last and last pointers */ Node secLast = null; Node last = head; /* After this loop secLast contains address of second last node and last contains address of last node in Linked List */ while (last.next != null) { secLast = last; last = last.next; } // Set the next of second last as null secLast.next = null; // Set the next of last as head last.next = head; // Change head to point to // last node. head = last; } // Utility functions /* Inserts a new Node at front of the list. */ public void push(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); // 3. Make next of new Node as head new_node.next = head; // 4. Move the head to point to // new Node head = new_node; } // Function to print linked list void printList() { Node temp = head; while(temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } // Driver code public static void main(String args[]) { LinkedList llist = new LinkedList(); /* Constructed Linked List is 1->2->3->4->5->null */ llist.push(5); llist.push(4); llist.push(3); llist.push(2); llist.push(1); System.out.println( "Linked List before moving last to front "); llist.printList(); llist.moveToFront(); System.out.println( "Linked List after moving last to front "); llist.printList(); } } // This code is contributed by Rajat Mishra Output: Linked list before moving last to front 1 2 3 4 5 Linked list after removing last to front 5 1 2 3 4 Time Complexity: O(n) where n is the number of nodes in the given Linked List. Space Complexity: O(1) because using constant variables Please refer complete article on Move last element to front of a given Linked List for more details! Comment More infoAdvertise with us Next Article Java Program for Merge Sort for Linked Lists K kartik Follow Improve Article Tags : Java Linked Lists Practice Tags : Java Similar Reads How to Add Element at First and Last Position of LinkedList in Java? LinkedList is a part of Collection framework present inside java.util package. This class is an implementation of LinkedList data structure which is a linear data structure where the elements are not stored in a contiguous manner and every element is a separate object with a data field and address f 2 min read Javascript Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. JavaScript// Linked List Class // Head of list let head; // Node 7 min read Java Program for Merge Sort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Let head be the first node of the linked list to be sorted and headRef be 4 min read Javascript Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where 7 min read Queue Implementation Using Linked List in Java Queue is the linear data structure that follows the First In First Out(FIFO) principle where the elements are added at the one end, called the rear, and removed from the other end, called the front. Using the linked list to implement the queue allows for dynamic memory utilization, avoiding the cons 4 min read LinkedList addFirst() Method in Java In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning.Syntax of LinkedList addFirst() Method public void addFirst( E e)Parameters: E is the data type of elemen 1 min read Like