Move last m elements to the front of a given Linked List
Last Updated :
10 Nov, 2022
Given the head of a Singly Linked List and a value m, the task is to move the last m elements to the front.
Examples:
Input: 4->5->6->1->2->3 ; m = 3
Output: 1->2->3->4->5->6
Input: 0->1->2->3->4->5 ; m = 4
Output: 2->3->4->5->0->1
Algorithm:
- Use two pointers: one to store the address of the last node and other for the address of the first node.
- Traverse the list till the first node of last m nodes.
- Maintain two pointers p, q i.e., p as the first node of last m nodes & q as just before node of p.
- Make the last node next as the original list head.
- Make the next of node q as NULL.
- Set the p as the head.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* next;
} * first, *last;
int length = 0;
void printList( struct Node* node)
{
while (node != NULL)
{
cout << node->data << " " ;
node = node->next;
}
}
void moveToFront( struct Node* head,
struct Node* p, int m)
{
if (head == NULL)
return ;
p = head;
head = head->next;
m++;
if (length == m)
{
p->next = NULL;
last->next = first;
first = head;
}
else
moveToFront(head, p, m);
}
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;
if (length == 0)
last = *head_ref;
else
first = *head_ref;
length++;
}
int main()
{
struct Node* start = NULL;
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
push(&start, 0);
cout << "Initial Linked list\n" ;
printList(start);
int m = 4;
struct Node* temp;
moveToFront(start, temp, m);
cout << "\n Final Linked list\n" ;
start = first;
printList(start);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
} * first, *last;
int length = 0;
void printList( struct Node* node)
{
while (node != NULL) {
printf ( "%d " , node->data);
node = node->next;
}
}
void moveToFront( struct Node* head,
struct Node* p, int m)
{
if (head == NULL)
return ;
p = head;
head = head->next;
m++;
if (length == m) {
p->next = NULL;
last->next = first;
first = head;
}
else
moveToFront(head, p, m);
}
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;
if (length == 0)
last = *head_ref;
else
first = *head_ref;
length++;
}
int main()
{
struct Node* start = NULL;
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
push(&start, 0);
printf ( "\n Initial Linked list\n" );
printList(start);
int m = 4;
struct Node* temp;
moveToFront(start, temp, m);
printf ( "\n Final Linked list\n" );
start = first;
printList(start);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
}
static Node first, last;
static int length = 0 ;
static void printList(Node node)
{
while (node != null )
{
System.out.printf( "%d " , node.data);
node = node.next;
}
}
static void moveToFront(Node head, Node p, int m)
{
if (head == null )
return ;
p = head;
head = head.next;
m++;
if (length == m)
{
p.next = null ;
last.next = first;
first = head;
}
else
moveToFront(head, p, m);
}
static Node 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;
if (length == 0 )
last = head_ref;
else
first = head_ref;
length++;
return head_ref;
}
public static void main(String[] args)
{
Node start = null ;
start = push(start, 5 );
start = push(start, 4 );
start = push(start, 3 );
start = push(start, 2 );
start = push(start, 1 );
start = push(start, 0 );
System.out.printf( "\n Initial Linked list\n" );
printList(start);
int m = 4 ;
Node temp = new Node();
moveToFront(start, temp, m);
System.out.printf( "\n Final Linked list\n" );
start = first;
printList(start);
}
}
|
Python3
class Node :
def __init__( self ):
self .data = 0
self . next = None
first = None
last = None
length = 0
def printList( node):
while (node ! = None ) :
print ( node.data, end = " " )
node = node. next
def moveToFront( head, p, m):
global first
global last
global length
if (head = = None ):
return head
p = head
head = head. next
m = m + 1
if (length = = m) :
p. next = None
last. next = first
first = head
else :
moveToFront(head, p, m)
def push( head_ref, new_data):
global first
global last
global length
new_node = Node()
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
if (length = = 0 ):
last = head_ref
else :
first = head_ref
length = length + 1
return head_ref
start = None
start = push(start, 5 )
start = push(start, 4 )
start = push(start, 3 )
start = push(start, 2 )
start = push(start, 1 )
start = push(start, 0 )
print ( "\n Initial Linked list" )
printList(start)
m = 4
temp = None
moveToFront(start, temp, m)
print ( "\n Final Linked list" )
start = first
printList(start)
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
}
static Node first, last;
static int length = 0;
static void printList(Node node)
{
while (node != null )
{
Console.Write( "{0} " , node.data);
node = node.next;
}
}
static void moveToFront(Node head,
Node p, int m)
{
if (head == null )
return ;
p = head;
head = head.next;
m++;
if (length == m)
{
p.next = null ;
last.next = first;
first = head;
}
else
moveToFront(head, p, m);
}
static Node 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;
if (length == 0)
last = head_ref;
else
first = head_ref;
length++;
return head_ref;
}
public static void Main(String[] args)
{
Node start = null ;
start = push(start, 5);
start = push(start, 4);
start = push(start, 3);
start = push(start, 2);
start = push(start, 1);
start = push(start, 0);
Console.Write( "Initial Linked list\n" );
printList(start);
int m = 4;
Node temp = new Node();
moveToFront(start, temp, m);
Console.Write( "\nFinal Linked list\n" );
start = first;
printList(start);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var first, last;
var length = 0;
function printList(node) {
while (node != null ) {
document.write(node.data + " " );
node = node.next;
}
}
function moveToFront(head, p, m) {
if (head == null ) return ;
p = head;
head = head.next;
m++;
if (length == m) {
p.next = null ;
last.next = first;
first = head;
} else moveToFront(head, p, m);
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
if (length == 0) last = head_ref;
else first = head_ref;
length++;
return head_ref;
}
var start = null ;
start = push(start, 5);
start = push(start, 4);
start = push(start, 3);
start = push(start, 2);
start = push(start, 1);
start = push(start, 0);
document.write( "Initial Linked list <br>" );
printList(start);
var m = 4;
var temp = new Node();
moveToFront(start, temp, m);
document.write( "<br> Final Linked list <br>" );
start = first;
printList(start);
</script>
|
Output:
Initial Linked list
0 1 2 3 4 5
Final Linked list
2 3 4 5 0 1
Time Complexity: O(n), where n represents the length of the list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...