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 < 6; i++) {
    head = push(head, i);
}
console.log("Original linked list:");
display(head);
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) return head;
    
    // Perform k rotations
    while (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 < 6; i++) {
    head1 = push(head1, i);
}

console.log("Before rotation:");
display(head1);

head1 = rotateOneByOne(head1, 3);
console.log("After rotating by 3:");
display(head1);
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 <= 0) return head;
    
    // Find length and last node
    var length = 1;
    var last = head;
    while (last.next) {
        last = last.next;
        length++;
    }
    
    // Handle k greater than length
    k = k % length;
    if (k == 0) return head;
    
    // Find the (length - k)th node
    var newTail = head;
    for (var i = 1; i < length - k; i++) {
        newTail = newTail.next;
    }
    
    // New head is the next node
    var newHead = newTail.next;
    
    // Break the connection
    newTail.next = null;
    
    // Connect last to original head
    last.next = head;
    
    return newHead;
}

// Test optimized function
var head2 = null;
for (var i = 1; i < 6; i++) {
    head2 = push(head2, i);
}

console.log("Before optimized rotation:");
display(head2);

head2 = rotateOptimized(head2, 2);
console.log("After rotating by 2 (optimized):");
display(head2);
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

505 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements