If I have a LinkedList:
Value A B C D E F
Index 0 1 2 3 4 5
I want to reorder the list or an Iterator for example at D(index 3) to be the new head but keep the order like:
Value D E F A B C
Index 0 1 2 3 4 5
Is this a functionality that can be achieved with LinkedList out of the box, basically realizing a ring? Is there another class that offers this already implemented?
You can use Collections.rotate for classes that implement the List interface:
List<String> list = Arrays.asList("A", "B", "C", "D", "E", "F");
Collections.rotate(list, 3);
System.out.println(list);
Output:
[D, E, F, A, B, C]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With