I'm supposed to create a method fx(int index1, int index2, int index3) where the nodes of the linked list from index1 to index2 are moved after node at index3 at the correct order. I think the problem is at the second while loop but I can't figure out how to fix it. Also I'm not sure about the part that prints out the elements
public void fx(int index1, int index2, int index3) {
Node<T> current = head;
int index =0;
Node<T> node3=null;
Node<T> node1=null;
Node<T> node2=null;
Node<T> beforeNode1=null;
Node<T> afterNode3=null;
Node<T> afterNode1=null;
while(current != null) {
if(index==index1-1) {
beforeNode1=current;
}
if(index==index1) {
node1 = current;
}
if(index==index2) {
node2=current;
}
if(index==index3) {
node3=current;
}
index++;
current=current.next;
}
node3.next=afterNode3;
node1.next=afterNode1;
beforeNode1.next=node3;
node3.next=node1;
node1.next=afterNode1;
while(afterNode1 != null) { // -->the problem
for(index=index1; index<=index2;) {
afterNode1=afterNode1.next;
index++;
}
}
node2.next=afterNode3;
System.out.println();
Node<T> temp = head;
while(temp!= null) {
System.out.print(temp.data);
temp = temp.next;
}
}