Skip to main content

Clockwise rotation of linked list

Given a linked list which include N nodes. And given a number K which indicates clockwise direction to rotate linked list node. Our goal is to rotate linked list nodes in clockwise.

Rotating linked list node clockwise
 Example 1
 Input   :  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k :  3
 Output  :  4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL

 Example 2
 Input   :  4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k :  18
 Output  :  8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL
 [
    Note :
    1) k is larger the number of linked list node.
    First 7 rotation
    4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL [7 rotation]
    Second 7 rotation
    4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL [14 rotation]
    Next 4 rotation
    8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL [18 rotation]

    Hint : use modulo operator to get final rotation
 ]

Here given code implementation process.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment