JavaScript Program for Clockwise rotation of Linked List

In JavaScript, a clockwise rotation of a linked list moves the last k elements to the front. This operation is commonly used in data structure manipulations and algorithm problems.

Given a linked list and a rotation count k, we need to move the last k nodes to the beginning. For example, rotating 1 ? 2 ? 3 ? 4 ? 5 clockwise by 2 positions results in 4 ? 5 ? 1 ? 2 ? 3.

Structure of Linked List

First, we'll create a Node class and helper functions to build and display the linked list:

// Node class for linked list
class Node {
    constructor(value = 0) {
        this.value = value;
        this.next = null;
    }
}

// Function to add elements to linked list
function push(head, data) {
    var newNode = new Node(data);
    if (head == null) {
        return newNode;
    }
    var temp = head;
    while (temp.next != null) {
        temp = temp.next;
    }
    temp.next = newNode;
    return head;
}

// Function to display linked list
function display(head) {
    var temp = head;
    var result = "";
    while (temp) {
        result += temp.value + " -> ";
        temp = temp.next;
    }
    console.log(result + "null");
}

// Create sample linked list: 1 -> 2 -> 3 -> 4 -> 5
var head = null;
for (var i = 1; i 

Original linked list:
1 -> 2 -> 3 -> 4 -> 5 -> null

Understanding Clockwise Rotation

Clockwise rotation means moving elements from the end to the beginning. Here's how it works:

Original: 1 -> 2 -> 3 -> 4 -> 5 -> null
Rotate by 1: 5 -> 1 -> 2 -> 3 -> 4 -> null
Rotate by 2: 4 -> 5 -> 1 -> 2 -> 3 -> null  
Rotate by 3: 3 -> 4 -> 5 -> 1 -> 2 -> null

Method 1: One-by-One Rotation

This approach moves one element at a time from the end to the front:

function rotateOneByOne(head, k) {
    if (!head || k  0) {
        // Find second last node
        var temp = head;
        while (temp.next.next != null) {
            temp = temp.next;
        }
        
        // Move last node to front
        var lastNode = temp.next;
        temp.next = null;
        lastNode.next = head;
        head = lastNode;
    }
    return head;
}

// Test the function
var head1 = null;
for (var i = 1; i 

Before rotation:
1 -> 2 -> 3 -> 4 -> 5 -> null
After rotating by 3:
3 -> 4 -> 5 -> 1 -> 2 -> null

Method 2: Optimized Single Pass Rotation

This efficient approach finds the rotation point and reconnects the list in one pass:

function rotateOptimized(head, k) {
    if (!head || !head.next || k 

Before optimized rotation:
1 -> 2 -> 3 -> 4 -> 5 -> null
After rotating by 2 (optimized):
4 -> 5 -> 1 -> 2 -> 3 -> null

Complexity Comparison

Method Time Complexity Space Complexity Efficiency
One-by-One O(N × K) O(1) Less efficient for large k
Optimized O(N) O(1) More efficient

Key Points

  • Handle edge cases: empty list, single node, k = 0
  • Use modulo operation when k > list length to avoid unnecessary rotations
  • The optimized approach traverses the list only twice maximum
  • Clockwise rotation moves elements from end to beginning

Conclusion

The optimized single-pass approach is preferred for clockwise linked list rotation with O(N) time complexity. It efficiently handles large rotation values and maintains constant space usage.

Updated on: 2026-03-15T23:19:01+05:30

489 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements