我在leetcode解决方案中看到过以下代码片段。它计算链表中元素的数量。while 语句只有一个赋值操作。谁能解释一下这是如何工作的。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
struct ListNode * temp = head;
int num = 1;
while(temp = temp->next){
num++;
}
temp->next
的值分配给temp
- 评估
while(temp)
(如temp != NULL
( - 在执行循环体的情况下
注意:如果head
为 NULL,您的程序将面临运行时错误