Last Updated : 23 Jul, 2025
Try it on GfG Practice
List is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. In this article, we are going to see how to rotate elements of a list. Let's consider the following list.
ORIGINAL LIST BEFORE ROTATIONThere are two types of rotations in a list. They are right rotation and left rotation. After Four Right Rotations the list becomes as shown below:
LIST AFTER ROTATING FOUR POSITIONSMethod 1: (Without Using in-built methods)
Working For Right Rotation
Example:
Java
// Java Program to Rotate Elements of the List
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// creating ArrayList
List<Integer> my_list = new ArrayList<>();
my_list.add(10);
my_list.add(20);
my_list.add(30);
my_list.add(40);
my_list.add(50);
my_list.add(60);
my_list.add(70);
// Printing list before rotation
System.out.println(
"List Before Rotation : "
+ Arrays.toString(my_list.toArray()));
// Loop according to the number of rotations
for (int i = 0; i < 4; i++) {
// storing the last element in the list
int temp = my_list.get(6);
// traverse the list and move elements to right
for (int j = 6; j > 0; j--) {
my_list.set(j, my_list.get(j - 1));
}
my_list.set(0, temp);
}
// Printing list after rotation
System.out.println(
"List After Rotation : "
+ Arrays.toString(my_list.toArray()));
}
}
List Before Rotation : [10, 20, 30, 40, 50, 60, 70] List After Rotation : [40, 50, 60, 70, 10, 20, 30]
After Four Left Rotations the list becomes as shown below:
LIST AFTER ROTATING FOUR POSITIONSWorking For Left Rotation
Example
Java
// Java Program to Rotate Elements of the List
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// creating array list
List<Integer> my_list = new ArrayList<>();
my_list.add(10);
my_list.add(20);
my_list.add(30);
my_list.add(40);
my_list.add(50);
my_list.add(60);
my_list.add(70);
// Printing list before rotation
System.out.println(
"List Before Rotation : "
+ Arrays.toString(my_list.toArray()));
// Loop according to the number of rotations
for (int i = 0; i < 4; i++) {
// storing the first element in the list
int temp = my_list.get(0);
// traverse the list and move elements to left
for (int j = 0; j < 6; j++) {
my_list.set(j, my_list.get(j + 1));
}
my_list.set(6, temp);
}
// Printing list after rotation
System.out.println(
"List After Rotation : "
+ Arrays.toString(my_list.toArray()));
}
}
List Before Rotation : [10, 20, 30, 40, 50, 60, 70] List After Rotation : [50, 60, 70, 10, 20, 30, 40]
Method 2: (Rotation Using Collections.rotate(list, distance) method)
Both left and right rotations can be performed directly using Java Collections.
Syntax
Collections.rotate(list_name , distance)
Parameters:
Returns: It returns a rotated list.
Note: Negative Distance gives Left Rotation while Positive gives Right Rotation.
Example: Using Positive distance to get the Right Rotation.
Java
// Java Program to Rotate Elements of the List
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// creating array list
List<Integer> my_list = new ArrayList<>();
my_list.add(10);
my_list.add(20);
my_list.add(30);
my_list.add(40);
my_list.add(50);
my_list.add(60);
my_list.add(70);
// Printing list before rotation
System.out.println(
"List Before Rotation : "
+ Arrays.toString(my_list.toArray()));
// Rotating the list at distance 4
Collections.rotate(my_list, 4);
// Printing list after rotation
System.out.println(
"List After Rotation : "
+ Arrays.toString(my_list.toArray()));
}
}
List Before Rotation : [10, 20, 30, 40, 50, 60, 70] List After Rotation : [40, 50, 60, 70, 10, 20, 30]
Example: Using Negative distance to get the Left Rotation.
Java
// Java Program to Rotate Elements of the List
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// creating array list
List<Integer> my_list = new ArrayList<>();
my_list.add(10);
my_list.add(20);
my_list.add(30);
my_list.add(40);
my_list.add(50);
my_list.add(60);
my_list.add(70);
// Printing list before rotation
System.out.println(
"List Before Rotation : "
+ Arrays.toString(my_list.toArray()));
// Rotating the list at distance -3
Collections.rotate(my_list, -4);
// Printing list after rotation
System.out.println(
"List After Rotation : "
+ Arrays.toString(my_list.toArray()));
}
}
List Before Rotation : [10, 20, 30, 40, 50, 60, 70] List After Rotation : [50, 60, 70, 10, 20, 30, 40]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4