Python 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). 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 Python Program For Moving Last Element To Front Of A Given Linked List K kartik Follow Improve Article Tags : Linked List Python Python Programs DSA Linked Lists Python-DSA +2 More Practice Tags : Linked Listpython Similar Reads Python Program For Finding The Middle Element Of A Given Linked List Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list 4 min read Python 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 special pointer to remove the required node from the linked list. Python3 # 6 min read Python 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 Python Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read Python 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 2 min read Like