简体   繁体   English

指针如何在链表中工作(Java)

[英]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.我对上面的 while 循环很困惑。 I couldn't tell how this while loop would do to the linkedlist poly1.我不知道这个 while 循环将如何处理链表 poly1。 Please help me out!请帮帮我!

Inside the while loop, the 1st line create a copy of the 'iter1.next'.在 while 循环中,第一行创建了“iter1.next”的副本。 the 2nd line makes the 'iter1' points to the 'poly1'.第二行使“iter1”指向“poly1”。 the 3rd line let 'poly1' become 'iter1'.第三行让'poly1'变成'iter1'。 the 4th line let iter1 become the 'next'.第 4 行让 iter1 成为“下一个”。

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 .将当前节点的下一个节点的引用存储到变量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.更新对当前节点的下一个节点的引用,以引用与引用poly1当前相同的 object 实例。 Note that poly1 starts with null as value.请注意, poly1null作为值开头。
  3. poly1 = iter1; updates the poly1 reference to reference the same object instance as the iter1 reference.更新poly1引用以引用与iter1引用相同的 object 实例。
  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 ).更新iter1引用以引用与next引用相同的 object 实例(旧的iter1.next引用,在它更改为poly1之前)。

I'll try to draw the steps as beautiful ASCII art:我将尝试将步骤绘制为美丽的 ASCII 艺术:

We'll start with a simple linked list with 3 nodes (ie A.next == B; B.next == C; C.next == null; ).我们将从一个包含 3 个节点的简单链表开始(即A.next == B; B.next == C; C.next == null; )。 We start with iter1 referencing the first node and poly1 referencing "nothing" (ie null ):我们从引用第一个节点的iter1和引用“无”的poly1开始(即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 ): while 循环的下一次迭代( 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 ): while 循环的下一次迭代( 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. iter1 != null现在为假,循环终止。 Your function effectively reverses the linked list by prepending all nodes to a new list;您的 function 通过将所有节点添加到新列表来有效地反转链表; I hope its name is reverse() .我希望它的名字是reverse()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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