Java Program For Moving Last Element To Front Of A Given Linked List Last Updated : 03 Aug, 2022 Comments Improve Suggest changes 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 Moving Last Element To Front Of A Given Linked List kartik Follow Improve Article Tags : Linked List Java Java Programs DSA Linked Lists +1 More Practice Tags : JavaLinked List Similar Reads Java Program For Finding The Middle Element Of A Given Linked List Given a Singly linked list, find the middle of the linked list. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. Example of Finding Middle Element of Linked ListInput: 1->2->3->4->5 Output: 3Â Input: 1->2->3->4->5-> 6 min read Java Program to Get the First and the Last Element of a Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object whi 4 min read Java Program For Deleting Last Occurrence Of An Item From Linked List Using pointers, loop through the whole list and keep track of the node prior to the node containing the last occurrence key using a special pointer. After this just store the next of next of the special pointer, into to next of special pointer to remove the required node from the linked list. Java / 6 min read Java Program For Moving All Occurrences Of An Element To End In A Linked List Given a linked list and a key in it, the task is to move all occurrences of the given key to the end of the linked list, keeping the order of all other elements the same. Examples: Input : 1 -> 2 -> 2 -> 4 -> 3 key = 2 Output : 1 -> 4 -> 3 -> 2 -> 2 Input : 6 -> 6 -> 7 6 min read Remove First and Last Elements from LinkedList in Java The Java.util.LinkedList.removeFirst() method is used to remove the first element from the LinkedList. The Java.util.LinkedList.removeLast() method is used to remove the last element from the LinkedList. Both the methods also returns the element after removing it. 1. removeFirst() Syntax: LinkedList 2 min read Java Program For Pairwise Swapping Elements Of A Given Linked List Given a singly linked list, write a function to swap elements pairwise. Input: 1->2->3->4->5->6->NULL Output: 2->1->4->3->6->5->NULL Input: 1->2->3->4->5->NULL Output: 2->1->4->3->5->NULL Input: 1->NULL Output: 1->NULL For examp 3 min read Java Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g 4 min read Adding an Element to the Front of LinkedList in Java A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements are linked using pointers and addresses. Each element is known as a node. This article shows how to add an element to the front of LinkedList in Java. Method 1: (Using user-def 3 min read Java Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read Java Program For Printing Nth Node From The End Of A Linked List Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below the list and n = 3, then the output is "B". Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Method 1 (Use 5 min read Like