Open In App

Javascript Program for Clockwise rotation of Linked List

Last Updated : 31 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.
Examples: 
 

Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, K = 2 
Output: 4 -> 5 -> 1 -> 2 -> 3 -> NULL
Input: 7 -> 9 -> 11 -> 13 -> 3 -> 5 -> NULL, K = 12 
Output: 7 -> 9 -> 11 -> 13 -> 3 -> 5 -> NULL 
 

 

Approach: To rotate the linked list first check whether the given k is greater than the count of nodes in the linked list or not. Traverse the list and find the length of the linked list then compare it with k, if less then continue otherwise deduce it in the range of linked list size by taking modulo with the length of the list. 
After that subtract the value of k from the length of the list. Now, the question has been changed to the left rotation of the linked list so follow that procedure: 
 

  • Change the next of the kth node to NULL.
  • Change the next of the last node to the previous head node.
  • Change the head to (k+1)th node.

In order to do that, the pointers to the kth node, (k+1)th node, and last node are required.
Below is the implementation of the above approach: 
 

Javascript




<script>
 
// JavaScript implementation of the approach   
 
/* Link list node */
 class Node {
        constructor() {
            this.data = 0;
            this.next = null;
        }
    }
 
    /* A utility function to push a node */
    function push(head_ref , new_data) {
        /* allocate node */
      var new_node = new Node();
 
        /* put in the data */
        new_node.data = new_data;
 
        /* link the old list off the new node */
        new_node.next = (head_ref);
 
        /* move the head to point to the new node */
        (head_ref) = new_node;
        return head_ref;
    }
 
    /* A utility function to print linked list */
    function printList(node) {
        while (node != null) {
            document.write(node.data + " -> ");
            node = node.next;
        }
        document.write("null");
    }
 
    // Function that rotates the given linked list
    // clockwise by k and returns the updated
    // head pointer
    function rightRotate(head , k) {
 
        // If the linked list is empty
        if (head == null)
            return head;
 
        // len is used to store length
        // of the linked list
        // tmp will point to the last
        // node after this loop
        var tmp = head;
        var len = 1;
        while (tmp.next != null) {
            tmp = tmp.next;
            len++;
        }
 
        // If k is greater than the size
        // of the linked list
        if (k > len)
            k = k % len;
 
        // Subtract from length to convert
        // it into left rotation
        k = len - k;
 
        // If no rotation needed then
        // return the head node
        if (k == 0 || k == len)
            return head;
 
        // current will either point to
        // kth or null after this loop
        var current = head;
        var cnt = 1;
        while (cnt < k && current != null) {
            current = current.next;
            cnt++;
        }
 
        // If current is null then k is equal to the
        // count of nodes in the list
        // Don't change the list in this case
        if (current == null)
            return head;
 
        // current points to the kth node
        var kthnode = current;
 
        // Change next of last node to previous head
        tmp.next = head;
 
        // Change head to (k+1)th node
        head = kthnode.next;
 
        // Change next of kth node to null
        kthnode.next = null;
 
        // Return the updated head pointer
        return head;
    }
 
    // Driver code
     
 
        /*
         * The constructed linked list is: 1.2.3.4.5
         */
        var head = null;
        head = push(head, 5);
        head = push(head, 4);
        head = push(head, 3);
        head = push(head, 2);
        head = push(head, 1);
 
        var k = 2;
 
        // Rotate the linked list
        var updated_head = rightRotate(head, k);
 
        // Print the rotated linked list
        printList(updated_head);
 
// This code contributed by Rajput-Ji
 
</script>


Output: 

4 -> 5 -> 1 -> 2 -> 3 -> NULL

 

Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of nodes in the linked list.

Auxiliary Space: O(1), as we are not using any extra space.
 

Please refer complete article on Clockwise rotation of Linked List for more details!
 



Previous Article
Next Article

Similar Reads

C++ Program for Clockwise rotation of Linked List
Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; NULL, K = 2 Output: 4 -&gt; 5 -&gt; 1 -&gt; 2 -&gt; 3 -&gt; NULLInput: 7 -&gt; 9 -&gt; 11 -&gt; 13 -&gt; 3 -&gt; 5 -&gt; NULL, K = 12 Output: 7 -&gt; 9 -&gt; 11 -&gt; 13 -&gt;
4 min read
Java Program for Clockwise rotation of Linked List
Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; NULL, K = 2 Output: 4 -&gt; 5 -&gt; 1 -&gt; 2 -&gt; 3 -&gt; NULLInput: 7 -&gt; 9 -&gt; 11 -&gt; 13 -&gt; 3 -&gt; 5 -&gt; NULL, K = 12 Output: 7 -&gt; 9 -&gt; 11 -&gt; 13 -&gt;
4 min read
Python3 Program for Clockwise rotation of Linked List
Write a Python3 program for a given singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; NULL, K = 2 Output: 4 -&gt; 5 -&gt; 1 -&gt; 2 -&gt; 3 -&gt; NULLInput: 7 -&gt; 9 -&gt; 11 -&gt; 13 -&gt; 3 -&gt; 5 -&gt; NULL, K = 12 Output: 7 -
5 min read
Clockwise rotation of Linked List
Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; NULL, K = 2 Output: 4 -&gt; 5 -&gt; 1 -&gt; 2 -&gt; 3 -&gt; NULLInput: 7 -&gt; 9 -&gt; 11 -&gt; 13 -&gt; 3 -&gt; 5 -&gt; NULL, K = 12 Output: 7 -&gt; 9 -&gt; 11 -&gt; 13 -&gt;
17 min read
Clockwise rotation of Doubly Linked List by N places
Given a doubly-linked list and an integer N, the task is to rotate the linked list clockwise by N nodes.Examples: Input: N = 2 Output: Approach: To rotate the Doubly linked list first check whether the given N is greater than the length of the list or not. If N is greater than the size of the list then deduce it in the range of linked list size by
16 min read
Javascript Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
3 min read
C++ Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
5 min read
Java Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
6 min read
Python3 Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
3 min read
Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n) Right (Or clockwise) rotate the given string by d elements (where d &lt;= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "
18 min read