0

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;
    }
}
Cedi
  • 9
  • 5
  • That code doesn't even compile. --- Once you fix it so it compiles, use a **debugger** to find any problem. – Andreas Jun 07 '20 at 11:56
  • it does compile. the result is just not correct – Cedi Jun 07 '20 at 11:57
  • Nope, does not compile: *index cannot be resolved to a variable*. --- Please see "[How to create a Minimal, **Reproducible** Example](https://stackoverflow.com/help/minimal-reproducible-example)". --- See also "[What is a **debugger** and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149)" and/or "[How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)". – Andreas Jun 07 '20 at 11:58
  • You're right i'm sorry. I translated the code in English before posting but I had forgotten to translate the 'index'. – Cedi Jun 07 '20 at 12:06
  • So did you try to **debug** your code? --- If you did, you might end up asking yourself: 1) Why are `afterNode3` and `afterNode1` always null? 2) Why is `node1.next` being updated? 3) Why is there a `while(afterNode1 != null)` loop at all? --- Perhaps you should take a step back and draw it on paper. – Andreas Jun 07 '20 at 12:23
  • I know it doesnt make sense i just dont know how to do it :( – Cedi Jun 07 '20 at 13:15
  • Then I'll say it again, highlighted so you can see it: **Perhaps you should take a step back and draw it on paper.** Then you will realize that you only need to update up to 3 `next` values, and maybe the `head` value if `index1 = 0`. --- Also consider what should happen if `index1`, `index2`, and `index3` are not in ascending order, or if they are negative or exceeds the list size. – Andreas Jun 07 '20 at 13:18

0 Answers0