Rotate the sub | Python3 implementation of the above approach ; Definition of node of linkedlist ; This function take head pointer of list , start and end points of sublist that is to be rotated and the number k and rotate the sublist to right by k places . ; If k is greater than size of sublist then we will take its modulo with size of sublist ; If k is zero or k is equal to size or k is a multiple of size of sublist then list remains intact ; m - th node ; This loop will traverse all node till end node of sublist . Current traversed node ; Count of traversed nodes ; Previous of m - th node ; We will save ( m - 1 ) th node and later make it point to ( n - k + 1 ) th node ; That is how we bring ( n - k + 1 ) th node to front of sublist . ; This keeps rest part of list intact . ; Function for creating and linking new nodes ; Driver code
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def rotateSubList ( A , m , n , k ) : NEW_LINE INDENT size = n - m + 1 NEW_LINE if ( k > size ) : NEW_LINE INDENT k = k % size NEW_LINE DEDENT if ( k == 0 or k == size ) : NEW_LINE INDENT head = A NEW_LINE while ( head != None ) : NEW_LINE INDENT print ( head . data ) NEW_LINE head = head . next NEW_LINE DEDENT return NEW_LINE DEDENT link = None NEW_LINE if ( m == 1 ) : NEW_LINE INDENT link = A NEW_LINE DEDENT c = A NEW_LINE count = 0 NEW_LINE end = None NEW_LINE pre = None NEW_LINE while ( c != None ) : NEW_LINE INDENT count = count + 1 NEW_LINE if ( count == m - 1 ) : NEW_LINE INDENT pre = c NEW_LINE link = c . next NEW_LINE DEDENT if ( count == n - k ) : NEW_LINE INDENT if ( m == 1 ) : NEW_LINE INDENT end = c NEW_LINE A = c . next NEW_LINE DEDENT else : NEW_LINE INDENT end = c NEW_LINE pre . next = c . next NEW_LINE DEDENT DEDENT if ( count == n ) : NEW_LINE INDENT d = c . next NEW_LINE c . next = link NEW_LINE end . next = d NEW_LINE head = A NEW_LINE while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT return NEW_LINE DEDENT c = c . next NEW_LINE DEDENT DEDENT def push ( head , val ) : NEW_LINE INDENT new_node = Node ( val ) NEW_LINE new_node . data = val NEW_LINE new_node . next = head NEW_LINE head = new_node NEW_LINE return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 70 ) NEW_LINE head = push ( head , 60 ) NEW_LINE head = push ( head , 50 ) NEW_LINE head = push ( head , 40 ) NEW_LINE head = push ( head , 30 ) NEW_LINE head = push ( head , 20 ) NEW_LINE head = push ( head , 10 ) NEW_LINE tmp = head NEW_LINE print ( " Given ▁ List : ▁ " , end = " " ) NEW_LINE while ( tmp != None ) : NEW_LINE INDENT print ( tmp . data , end = " ▁ " ) NEW_LINE tmp = tmp . next NEW_LINE DEDENT print ( ) NEW_LINE m = 3 NEW_LINE n = 6 NEW_LINE k = 2 NEW_LINE print ( " After ▁ rotation ▁ of ▁ sublist : ▁ " , end = " " ) NEW_LINE rotateSubList ( head , m , n , k ) NEW_LINE DEDENT