Move last element to front of a given Linked List
Write a function that moves the last node to the front in a given Singly Linked List.
Examples:
Input: 1->2->3->4->5
Output: 5->1->2->3->4
Input: 3->8->1->5->7->12
Output: 12->3->8->1->5->7
Approach: To solve the problem follow the below idea:
Traverse the list till the last node. Use two pointers: one to store the address of the last node and other for the address of the 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
Follow the given steps to solve the problem using the above approach:
- Traverse the linked list till the last node and Initialize two pointers to store the address of the last and the second last node
- Then follow these three steps to move the last node to the front
- 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)
Below is the implementation of the above approach:
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void moveToFront( struct Node** head_ref)
{
if (*head_ref == NULL || (*head_ref)->next == NULL)
return ;
struct Node* secLast = NULL;
struct Node* last = *head_ref;
while (last->next != NULL) {
secLast = last;
last = last->next;
}
secLast->next = NULL;
last->next = *head_ref;
*head_ref = last;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* node)
{
while (node != NULL) {
printf ( "%d " , node->data);
node = node->next;
}
}
int main()
{
struct Node* start = NULL;
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
printf ( "Linked list before moving last to front\n" );
printList(start);
moveToFront(&start);
printf ( "\nLinked list after removing last to front\n" );
printList(start);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void moveToFront(Node** head_ref)
{
if (*head_ref == NULL || (*head_ref)->next == NULL)
return ;
Node* secLast = NULL;
Node* last = *head_ref;
while (last->next != NULL) {
secLast = last;
last = last->next;
}
secLast->next = NULL;
last->next = *head_ref;
*head_ref = last;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
int main()
{
Node* start = NULL;
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
cout << "Linked list before moving last to front\n" ;
printList(start);
moveToFront(&start);
cout << "\nLinked list after removing last to front\n" ;
printList(start);
return 0;
}
|
Java
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void moveToFront()
{
if (head == null || head.next == null )
return ;
Node secLast = null ;
Node last = head;
while (last.next != null ) {
secLast = last;
last = last.next;
}
secLast.next = null ;
last.next = head;
head = last;
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
public static void main(String args[])
{
LinkedList llist = new LinkedList();
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();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , data):
new_node = Node(data)
new_node. next = self .head
self .head = new_node
def printList( self ):
tmp = self .head
while tmp is not None :
print (tmp.data, end = ", " )
tmp = tmp. next
print ()
def moveToFront( self ):
tmp = self .head
sec_last = None
if not tmp or not tmp. next :
return
while tmp and tmp. next :
sec_last = tmp
tmp = tmp. next
sec_last. next = None
tmp. next = self .head
self .head = tmp
if __name__ = = '__main__' :
llist = LinkedList()
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()
|
C#
using System;
class LinkedList {
Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
void moveToFront()
{
if (head == null || head.next == null )
return ;
Node secLast = null ;
Node last = head;
while (last.next != null ) {
secLast = last;
last = last.next;
}
secLast.next = null ;
last.next = head;
head = last;
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);
Console.WriteLine(
"Linked List before moving last to front " );
llist.printList();
llist.moveToFront();
Console.WriteLine(
"Linked List after moving last to front " );
llist.printList();
}
}
|
Javascript
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head;
function moveToFront() {
if (head == null || head.next == null )
return ;
var secLast = null ;
var last = head;
while (last.next != null ) {
secLast = last;
last = last.next;
}
secLast.next = null ;
last.next = head;
head = last;
}
function push(new_data) {
var new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function printList() {
var temp = head;
while (temp != null ) {
document.write(temp.data + " " );
temp = temp.next;
}
document.write();
}
push(5);
push(4);
push(3);
push(2);
push(1);
document.write( "Linked List before moving last to front<br/> " );
printList();
moveToFront();
document.write( "<br/>Linked List after moving last to front <br/>" );
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), As we need to traverse the list once.
Auxiliary Space: O(1), As constant extra space is used.
Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...