Rotate the sub-list of a linked list from position M to N to the right by K places
Given a linked list and two positions ‘m’ and ‘n’. The task is to rotate the sublist from position m to n, to the right by k places.
Examples:
Input: list = 1->2->3->4->5->6, m = 2, n = 5, k = 2
Output: 1->4->5->2->3->6
Rotate the sublist 2 3 4 5 towards right 2 times
then the modified list are: 1 4 5 2 3 6
Input: list = 20->45->32->34->22->28, m = 3, n = 6, k = 3
Output: 20->45->34->22->28->32
Rotate the sublist 32 34 22 28 towards right 3 times
then the modified list are: 20 45 34 22 28 32
Approach: For rotating the given sublist that extends from m to n element, move the list from (n-k+1)th to nth node to starting of sub-list to finish the rotation.
If k is greater than size of sublist then we will take its modulo with size of sublist. So traverse through list using a pointer and a counter and we will save (m-1)th node and later make it point to (n-k+1)th node and hence bring (n-k+1)th node to the start(front) of sublist.
Similarly we will save mth node and later make nth node point to it. And for keeping rest of list intact we will make (n-k)th node point to next node of n (maybe NULL). And finally we will get the k times right rotated sublist.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int data;
struct ListNode* next;
};
void rotateSubList(ListNode* A, int m, int n, int k)
{
int size = n - m + 1;
if (k > size) {
k = k % size;
}
if (k == 0 || k == size) {
ListNode* head = A;
while (head != NULL) {
cout << head->data;
head = head->next;
}
return ;
}
ListNode* link = NULL;
if (m == 1) {
link = A;
}
ListNode* c = A;
int count = 0;
ListNode* end = NULL;
ListNode* pre = NULL;
while (c != NULL) {
count++;
if (count == m - 1) {
pre = c;
link = c->next;
}
if (count == n - k) {
if (m == 1) {
end = c;
A = c->next;
}
else {
end = c;
pre->next = c->next;
}
}
if (count == n) {
ListNode* d = c->next;
c->next = link;
end->next = d;
ListNode* head = A;
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
return ;
}
c = c->next;
}
}
void push( struct ListNode** head, int val)
{
struct ListNode* new_node = new ListNode;
new_node->data = val;
new_node->next = (*head);
(*head) = new_node;
}
int main()
{
struct ListNode* head = NULL;
push(&head, 70);
push(&head, 60);
push(&head, 50);
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
ListNode* tmp = head;
cout << "Given List: " ;
while (tmp != NULL) {
cout << tmp->data << " " ;
tmp = tmp->next;
}
cout << endl;
int m = 3, n = 6, k = 2;
cout << "After rotation of sublist: " ;
rotateSubList(head, m, n, k);
return 0;
}
|
Java
import java.util.*;
class Solution
{
static class ListNode {
int data;
ListNode next;
}
static void rotateSubList(ListNode A, int m, int n, int k)
{
int size = n - m + 1 ;
if (k > size) {
k = k % size;
}
if (k == 0 || k == size) {
ListNode head = A;
while (head != null ) {
System.out.print( head.data);
head = head.next;
}
return ;
}
ListNode link = null ;
if (m == 1 ) {
link = A;
}
ListNode c = A;
int count = 0 ;
ListNode end = null ;
ListNode pre = null ;
while (c != null ) {
count++;
if (count == m - 1 ) {
pre = c;
link = c.next;
}
if (count == n - k) {
if (m == 1 ) {
end = c;
A = c.next;
}
else {
end = c;
pre.next = c.next;
}
}
if (count == n) {
ListNode d = c.next;
c.next = link;
end.next = d;
ListNode head = A;
while (head != null ) {
System.out.print( head.data+ " " );
head = head.next;
}
return ;
}
c = c.next;
}
}
static ListNode push( ListNode head, int val)
{
ListNode new_node = new ListNode();
new_node.data = val;
new_node.next = (head);
(head) = new_node;
return head;
}
public static void main(String args[])
{
ListNode head = null ;
head =push(head, 70 );
head =push(head, 60 );
head =push(head, 50 );
head =push(head, 40 );
head =push(head, 30 );
head =push(head, 20 );
head =push(head, 10 );
ListNode tmp = head;
System.out.print( "Given List: " );
while (tmp != null ) {
System.out.print( tmp.data + " " );
tmp = tmp.next;
}
System.out.println();
int m = 3 , n = 6 , k = 2 ;
System.out.print( "After rotation of sublist: " );
rotateSubList(head, m, n, k);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def rotateSubList(A, m, n, k):
size = n - m + 1
if (k > size):
k = k % size
if (k = = 0 or k = = size):
head = A
while (head ! = None ):
print (head.data)
head = head. next
return
link = None
if (m = = 1 ) :
link = A
c = A
count = 0
end = None
pre = None
while (c ! = None ) :
count = count + 1
if (count = = m - 1 ) :
pre = c
link = c. next
if (count = = n - k) :
if (m = = 1 ) :
end = c
A = c. next
else :
end = c
pre. next = c. next
if (count = = n) :
d = c. next
c. next = link
end. next = d
head = A
while (head ! = None ) :
print (head.data, end = " " )
head = head. next
return
c = c. next
def push(head, val):
new_node = Node(val)
new_node.data = val
new_node. next = head
head = new_node
return head
if __name__ = = '__main__' :
head = None
head = push(head, 70 )
head = push(head, 60 )
head = push(head, 50 )
head = push(head, 40 )
head = push(head, 30 )
head = push(head, 20 )
head = push(head, 10 )
tmp = head
print ( "Given List: " , end = "")
while (tmp ! = None ) :
print (tmp.data, end = " " )
tmp = tmp. next
print ()
m = 3
n = 6
k = 2
print ( "After rotation of sublist: " , end = "")
rotateSubList(head, m, n, k)
|
C#
using System;
class GFG
{
public class ListNode
{
public int data;
public ListNode next;
}
public static void rotateSubList(ListNode A, int m,
int n, int k)
{
int size = n - m + 1;
if (k > size)
{
k = k % size;
}
if (k == 0 || k == size)
{
ListNode head = A;
while (head != null )
{
Console.Write(head.data);
head = head.next;
}
return ;
}
ListNode link = null ;
if (m == 1)
{
link = A;
}
ListNode c = A;
int count = 0;
ListNode end = null ;
ListNode pre = null ;
while (c != null )
{
count++;
if (count == m - 1)
{
pre = c;
link = c.next;
}
if (count == n - k)
{
if (m == 1)
{
end = c;
A = c.next;
}
else
{
end = c;
pre.next = c.next;
}
}
if (count == n)
{
ListNode d = c.next;
c.next = link;
end.next = d;
ListNode head = A;
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
return ;
}
c = c.next;
}
}
public static ListNode push(ListNode head, int val)
{
ListNode new_node = new ListNode();
new_node.data = val;
new_node.next = (head);
(head) = new_node;
return head;
}
public static void Main( string [] args)
{
ListNode head = null ;
head = push(head, 70);
head = push(head, 60);
head = push(head, 50);
head = push(head, 40);
head = push(head, 30);
head = push(head, 20);
head = push(head, 10);
ListNode tmp = head;
Console.Write( "Given List: " );
while (tmp != null )
{
Console.Write(tmp.data + " " );
tmp = tmp.next;
}
Console.WriteLine();
int m = 3, n = 6, k = 2;
Console.Write( "After rotation of sublist: " );
rotateSubList(head, m, n, k);
}
}
|
Javascript
<script>
class ListNode {
constructor() {
this .data = 0;
this .next = null ;
}
}
function rotateSubList(A, m, n, k) {
var size = n - m + 1;
if (k > size) {
k = k % size;
}
if (k == 0 || k == size) {
var head = A;
while (head != null ) {
document.write(head.data);
head = head.next;
}
return ;
}
var link = null ;
if (m == 1) {
link = A;
}
var c = A;
var count = 0;
var end = null ;
var pre = null ;
while (c != null ) {
count++;
if (count == m - 1) {
pre = c;
link = c.next;
}
if (count == n - k) {
if (m == 1) {
end = c;
A = c.next;
} else {
end = c;
pre.next = c.next;
}
}
if (count == n) {
var d = c.next;
c.next = link;
end.next = d;
var head = A;
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
return ;
}
c = c.next;
}
}
function push(head, val) {
var new_node = new ListNode();
new_node.data = val;
new_node.next = head;
head = new_node;
return head;
}
var head = null ;
head = push(head, 70);
head = push(head, 60);
head = push(head, 50);
head = push(head, 40);
head = push(head, 30);
head = push(head, 20);
head = push(head, 10);
var tmp = head;
document.write( "Given List: " );
while (tmp != null ) {
document.write(tmp.data + " " );
tmp = tmp.next;
}
document.write( "<br>" );
var m = 3,
n = 6,
k = 2;
document.write( "After rotation of sublist: " );
rotateSubList(head, m, n, k);
</script>
|
Output:
Given List: 10 20 30 40 50 60 70
After rotation of sublist: 10 20 50 60 30 40 70
Complexity Analysis:
- Time complexity: O(N) where N is the size of the given linked list
- Auxiliary Space: O(1)
Last Updated :
06 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...