Java Program for Clockwise rotation of Linked List Last Updated : 31 May, 2022 Comments Improve Suggest changes Like Article Like 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 -> NULLInput: 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: Java // Java implementation of the approach class GFG { /* Link list node */ static class Node { int data; Node next; } /* A utility function to push a node */ static Node push(Node head_ref, int new_data) { /* allocate node */ Node 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 */ static void printList(Node node) { while (node != null) { System.out.print(node.data + " -> "); node = node.next; } System.out.print( "null"); } // Function that rotates the given linked list // clockwise by k and returns the updated // head pointer static Node rightRotate(Node head, int 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 Node tmp = head; int 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 Node current = head; int 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 Node 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 public static void main(String args[]) { /* The constructed linked list is: 1.2.3.4.5 */ Node head = null; head = push(head, 5); head = push(head, 4); head = push(head, 3); head = push(head, 2); head = push(head, 1); int k = 2; // Rotate the linked list Node updated_head = rightRotate(head, k); // Print the rotated linked list printList(updated_head); } } // This code is contributed by Arnub Kundu 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! Comment More infoAdvertise with us Next Article Java Program for Clockwise rotation of Linked List kartik Follow Improve Article Tags : Linked List Java Data Structures Java Programs DSA Linked Lists rotation +3 More Practice Tags : Data StructuresJavaLinked List Similar Reads Java Program For Rotating A Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal 6 min read Java Program to Rotate Linked List block wise Given a Linked List of length n and block length k rotate in a circular manner towards right/left each block by a number d. If d is positive rotate towards right else rotate towards left. Examples:Â Input: 1->2->3->4->5->6->7->8->9->NULL, k = 3 d = 1 Output: 3->1->2->6->4->5->9->7->8->NULL Explanati 4 min read Java Program to Rotate Doubly linked list by N nodes Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list. N = 2Rotated List: Examples: Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d As 4 min read Java Program For QuickSort On Singly Linked List QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List. Recommended: Please solve it 3 min read Java Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, 5 min read Java Program for Reverse a linked list Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In 3 min read Java Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g 4 min read Java Program For Segregating Even And Odd Nodes In A Linked List Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NUL 6 min read Java Program For Pairwise Swapping Elements Of A Given Linked List Given a singly linked list, write a function to swap elements pairwise. Input: 1->2->3->4->5->6->NULL Output: 2->1->4->3->6->5->NULL Input: 1->2->3->4->5->NULL Output: 2->1->4->3->5->NULL Input: 1->NULL Output: 1->NULL For examp 3 min read Java Program to Implement Triply Linked List Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. In this post, methods to insert a new node in a linked list are discussed. A node can be inserted in three ways either at the front of the linked list or after a given node or at the 8 min read Like