Here is generic code for your requirement with complexity of O(n) -
<E> List<E> spliceAndReverse(List<E> list, int startIndex, int endIndex){
while(startIndex < endIndex){
E e = list.get(startIndex);
list.set(startIndex, list.get(endIndex));
list.set(endIndex, e);
startIndex++;
endIndex--;
}
return list;
}
We can also use sublist, here is code for that -
static <E> List<E> spliceAndReverseUsingSubList(List<E> list, int startIndex, int endIndex){
List<E> subList = list.subList(startIndex, endIndex+1);
Collections.reverse(subList);
List<E> resultList = new ArrayList<>(list.subList(0, startIndex));
resultList.addAll(subList);
if(list.size() != endIndex+1){
resultList.addAll(list.subList(endIndex+1, list.size()));
}
return resultList;
}
See Example here -
http://blog.deepaktripathi.in/uncategorized/reverse-arraylist-between-2-given-indexes/
Note - Before asking question on platform please make sure you have tried enough.