简体   繁体   中英

How do pointers work in a linked list (Java)

/**
 * 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.

It doesn't copy anything, it assigns the reference. All statements are executed in order. The assignment operator assigns the value of the right-hand side to the variable on the left-hand side. (It's not an equivalence relation as in mathemetics).

  1. PolyNode next = iter1.next; stores a reference to the next node of the current node into variable next .
  2. iter1.next = poly1; updates the reference to the next node of the current node to reference the same object instance that reference poly1 currently does. Note that poly1 starts with null as value.
  3. poly1 = iter1; updates the poly1 reference to reference the same object instance as the iter1 reference.
  4. iter1 = next; updates the iter1 reference to reference the same object instance as the next reference (the old iter1.next reference, before it got changed to poly1 ).

I'll try to draw the steps as beautiful ASCII art:

We'll start with a simple linked list with 3 nodes (ie A.next == B; B.next == C; C.next == null; ). We start with iter1 referencing the first node and poly1 referencing "nothing" (ie null ):

A->B->C->null
^------------ iter1
         ^--- poly1 (of course, this there isn't any single null)
  1. PolyNode next = iter1.next;

     A->B->C->null ^------------ iter1 ^--------- next == iter1.next
  2. iter1.next = poly1;

     A->null | B->C->null ^------------------- iter1 ^---------------- iter1.next == poly1 (== null) ^--------- next
  3. poly1 = iter1;

     A->null | B->C->null ^------------------- iter1 == poly1 ^---------------- iter1.next == poly1 (== null) ^--------- next
  4. iter1 = next;

     A->null | B->C->null ^------------------- poly1 ^--------- next == iter1 ^------ iter1.next

Next iteration of the while loop ( iter1 != null ):

  1. PolyNode next = iter1.next;

     A->null | B->C->null ^------------------- poly1 ^--------- iter1 ^------ iter1.next == next
  2. iter1.next = poly1;

     B->A->null | C->null ^------------------- iter1 ^---------------- poly1 == iter1.next ^------ next
  3. poly1 = iter1;

     B->A->null | C->null ^------------------- iter1 == poly1 ^---------------- iter1.next ^------ next
  4. iter1 = next;

     B->A->null | C->null ^------------------- poly1 ^------ next == iter1

Next iteration of the while loop ( iter1 != null ):

  1. PolyNode next = iter1.next;

     B->A->null | C->null ^------------------- poly1 ^------ iter1 ^--- iter1.next == next
  2. iter1.next = poly1;

     C->B->A->null ^------------ iter1 ^--------- poly1 == iter1.next ^--- next
  3. poly1 = iter1;

     C->B->A->null ^------------ iter1 == poly1 ^--------- iter1.next ^--- next
  4. iter1 = next;

     C->B->A->null ^------------ poly1 ^--- next == iter1

iter1 != null is now false and the loop terminates. Your function effectively reverses the linked list by prepending all nodes to a new list; I hope its name is reverse() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM