Python 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). Python3 # Python3 code to move the last item # to front class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None # Function to add a node # at the beginning of Linked List def push(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node # Function to print nodes in # a given linked list def printList(self): tmp = self.head while tmp is not None: print(tmp.data, end = ", ") tmp = tmp.next print() # Function to bring the last node # to the front def moveToFront(self): tmp = self.head # To maintain the track of # the second last node sec_last = None # To check whether we have not # received the empty list or list # with a single node if not tmp or not tmp.next: return # Iterate till the end to get # the last and second last node while tmp and tmp.next : sec_last = tmp tmp = tmp.next # Point the next of the second # last node to None sec_last.next = None # Make the last node as the # first Node tmp.next = self.head self.head = tmp # Driver Code if __name__ == '__main__': llist = LinkedList() # Swap the 2 nodes llist.push(5) llist.push(4) llist.push(3) llist.push(2) llist.push(1) print ( "Linked List before moving last to front ") llist.printList() llist.moveToFront() print ( "Linked List after moving last to front ") llist.printList() 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 Insert and delete at the beginning in Doubly Linked List in Python K kartik Follow Improve Article Tags : Python Linked Lists Python-DSA Practice Tags : python Similar Reads Implementation of XOR Linked List in Python Prerequisite: XOR Linked List An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory-efficient version of Doubly Linked List can be created using only one space for the address field with every node. This memory efficient Doub 6 min read Perform Append at Beginning of List - Python The task of appending an element to the beginning of a list involves adding a new item at the start of an existing list, shifting the other elements to the right. For example, if we have a list [1, 2, 3, 4] and we want to append the value 0 at the beginning, the resulting list would be [0, 1, 2, 3, 4 min read Implementation of Queue using Linked List in Python A queue is a linear data structure that follows the FIFO (First In First Out) principle. The first element added is the first one to be removed. With a queue, the least recently added item is removed first. A good example of a queue is any queue of consumers for a resource where the consumer that ca 4 min read Insert and delete at the beginning in Doubly Linked List in Python A doubly linked list is a collection of nodes. It is an advanced version of a singly linked list. In a doubly linked list, a node has three elements its data, a link to the next node, and a link to its previous node. In this article, we will learn how to insert a node at the beginning and delete a n 6 min read Python Library for Linked List Linked list is a simple data structure in programming, which obviously is used to store data and retrieve it accordingly. To make it easier to imagine, it is more like a dynamic array in which data elements are linked via pointers (i.e. the present record points to its next record and the next one p 5 min read dllist class of llist module in Python llist is an extension module of CPython that provides a basic linked list structure. They are significantly faster that dequeue and even the standard list for that matter. Doubly Linked List It is a type of linked list in each node stores data as well as two addresses (address of nodes succeeding an 4 min read Like