2

I tried printing below code (temp):

public void insert(int val, int index){
    if(index == 0){
        insertFirst(val);
        return; 
    }
    if(index == size){
        insertLast(val);
        return;
    }

    Node temp = head;

    for(int i=1; i<index; i++){
        temp = temp.next;
        System.out.println(temp);
    }
    Node node = new Node(val, temp.next);
    temp.next = node;
    size++;
}

But getting this output instead: enter image description here

please let me know, how to resolve it, i want to see the flow!

  • 1
    You could either explicitly print what you want to print (which I guess would be `temp.getValue()` or something), or better still, override the `toString` method in your `Node` class, so that `System.out.println` knows how to print them. – Dawood ibn Kareem Jun 09 '22 at 04:49
  • @DawoodibnKareem I tried the toString method earlier, but the error is still there, not converting Node to string! – Harsha Happyness Jun 09 '22 at 06:50

0 Answers0