/** * Definition for polynomial singly-linked list. * class PolyNode { * int coefficient, power; * PolyNode next = null; * PolyNode() {} * PolyNode(int x, int y) { this.coefficient = x; this.power = y; } * PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; } * } */ PolyNode iter1 = poly1; PolyNode poly1 = null; while(iter1 != null){ PolyNode next = iter1.next; iter1.next = poly1; poly1 = iter1; iter1 = next; } I'm very confused on the above while loop. I couldn't tell how this while loop would do to the linkedlist poly1. Please help me out!
Inside the while loop, the 1st line create a copy of the 'iter1.next'. the 2nd line makes the 'iter1' points to the 'poly1'. the 3rd line let 'poly1' become 'iter1'. the 4th line let iter1 become the 'next'.
Please correct where I got wrong, as I tried to draw the graph from the above logic. and it didn't quite make sense to me.