[Expected Approach] Traverse Till Last Node - O(n) Time and O(1) space:
Traverse the list till the last node. Use two pointers to store the reference of the last node and second last node. After the end of loop, make the second last node as the last node and the last node as the head node.
Below is the implementation of the above approach:
C++
// C++ Program to move last element// to front in a given linked list#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function to move the last node to the// front of the linked listNode*moveToFront(Node*head){// If the list is empty or has only one node,// no need to moveif(head==NULL||head->next==NULL){returnhead;}// To keep track of the second last nodeNode*secLast=NULL;// To traverse to the last nodeNode*last=head;// Traverse the list to find the last and// second last nodeswhile(last->next!=NULL){secLast=last;last=last->next;}// Change the next of second last node to NULLsecLast->next=NULL;// Make the last node as the new headlast->next=head;head=last;returnhead;}voidprintList(Node*node){while(node!=NULL){cout<<node->data<<" ";node=node->next;}cout<<endl;}intmain(){// Create a linked list 1->2->3->4->5Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);cout<<"Linked list before: "<<endl;printList(head);head=moveToFront(head);cout<<"Linked list after: "<<endl;printList(head);return0;}
C
// C Program to move last element// to front in a given linked list#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Function to move the last node to the front of the linked liststructNode*moveToFront(structNode*head){// If the list is empty or has only one node, no need to moveif(head==NULL||head->next==NULL){returnhead;}// To keep track of the second last nodestructNode*secLast=NULL;// To traverse to the last nodestructNode*last=head;// Traverse the list to find the last and second last nodeswhile(last->next!=NULL){secLast=last;last=last->next;}// Change the next of second last node to NULLsecLast->next=NULL;// Make the last node as the new headlast->next=head;head=last;returnhead;}voidprintList(structNode*node){while(node!=NULL){printf("%d ",node->data);node=node->next;}printf("\n");}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->next=NULL;returnnewNode;}intmain(){// Create a linked list 1->2->3->4->5structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(3);head->next->next->next=createNode(4);head->next->next->next->next=createNode(5);printf("Linked list before:\n");printList(head);head=moveToFront(head);printf("Linked list after:\n");printList(head);return0;}
Java
// Java Program to move last element// to front in a given linked listclassNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGfG{// Function to move the last node to the // front of the linked liststaticNodemoveToFront(Nodehead){// If the list is empty or has only one node, // no need to moveif(head==null||head.next==null){returnhead;}// To keep track of the second last nodeNodesecLast=null;// To traverse to the last nodeNodelast=head;// Traverse the list to find the last and // second last nodeswhile(last.next!=null){secLast=last;last=last.next;}// Change the next of second last node to nullsecLast.next=null;// Make the last node as the new headlast.next=head;head=last;returnhead;}staticvoidprintList(Nodenode){while(node!=null){System.out.print(node.data+" ");node=node.next;}System.out.println();}publicstaticvoidmain(String[]args){// Create a linked list 1->2->3->4->5Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);System.out.println("Linked list before: ");printList(head);head=moveToFront(head);System.out.println("Linked list after: ");printList(head);}}
Python
# Python code to move the last item to frontclassNode:def__init__(self,data):self.data=dataself.next=None# Function to move the last node # to the front of the linked listdefmove_to_front(head):# If the list is empty or has # only one node, no need to moveifheadisNoneorhead.nextisNone:returnhead# To keep track of the second last nodesec_last=None# To traverse to the last nodelast=head# Traverse the list to find the # last and second last nodeswhilelast.nextisnotNone:sec_last=lastlast=last.next# Change the next of second last node to Nonesec_last.next=None# Make the last node as the new headlast.next=headhead=lastreturnheaddefprint_list(node):whilenodeisnotNone:print(node.data,end=" ")node=node.nextprint()# Create a linked list 1->2->3->4->5head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)print("Linked list before:")print_list(head)head=move_to_front(head)print("Linked list after:")print_list(head)
C#
// C# Program to move last element// to front in a given linked listclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGfG{// Function to move the last node to the // front of the linked liststaticNodeMoveToFront(Nodehead){// If the list is empty or has only one node, // no need to moveif(head==null||head.next==null){returnhead;}// To keep track of the second last nodeNodesecLast=null;// To traverse to the last nodeNodelast=head;// Traverse the list to find the last and // second last nodeswhile(last.next!=null){secLast=last;last=last.next;}// Change the next of second last node to nullsecLast.next=null;// Make the last node as the new headlast.next=head;head=last;returnhead;}staticvoidPrintList(Nodenode){while(node!=null){System.Console.Write(node.data+" ");node=node.next;}System.Console.WriteLine();}publicstaticvoidMain(){// Create a linked list 1->2->3->4->5Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);System.Console.WriteLine("Linked list before:");PrintList(head);head=MoveToFront(head);System.Console.WriteLine("Linked list after:");PrintList(head);}}
JavaScript
// Javascript Program to move last element to// front in a given linked listclassNode{constructor(data){this.data=data;this.next=null;}}// Function to move the last // node to the front of the linked listfunctionmoveToFront(head){// If the list is empty or has // only one node, no need to moveif(head===null||head.next===null){returnhead;}// To keep track of the second last nodeletsecLast=null;// To traverse to the last nodeletlast=head;// Traverse the list to find the last // and second last nodeswhile(last.next!==null){secLast=last;last=last.next;}// Change the next of second last node to nullsecLast.next=null;// Make the last node as the new headlast.next=head;head=last;returnhead;}functionprintList(node){while(node!==null){process.stdout.write(node.data+" ");node=node.next;}console.log();}lethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);console.log("Linked list before:");printList(head);head=moveToFront(head);console.log("Linked list after:");printList(head);
Output
Linked list before:
1 2 3 4 5
Linked list after:
5 1 2 3 4
Time Complexity: O(n), As we need to traverse the list once. Auxiliary Space: O(1)