给定一个单链表实现和实例化:
class ListNode:
def __init__(self, val=0, next=None):
self.next = next
self.val = val
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)
head.next.next.next = ListNode(4)
Run Code Online (Sandbox Code Playgroud)
进行以下操作后:
current_node = head
current_node = current_node.next
current_node.next = current_node.next.next
Run Code Online (Sandbox Code Playgroud)
生成的链表为 2->4 current_node
,而 为 1->2->4 head
。的结果对current_node
我来说是有意义的,但我很困惑为什么结果head
会改变。在这 3 个操作之后,我预计head
会等于原来的值 (1->2->3->4),因为没有直接对head
. 但是,如果操作是这样的:
current_node = head
current_node.next = current_node.next.next
Run Code Online (Sandbox Code Playgroud)
我确实希望head
将其修改为 1->3->4,因为 和 都current_node
引用head
相同的内存地址。相反,在第一个示例中,在重新分配的第二行之后,current_node
不再引用与 相同的内存地址head
。我的理解是,无论发生什么,这个重新分配current_node
都不应再与之相关head
(显然情况并非如此)。有人可以解释一下 …
我将如何使用流来实现与下面相同的结果?我试图通过首先迭代 TickQueue (队列实现)并对 tot 求和,然后除以计数器值来找到平均值,从而找到此“tot”值的平均值。
int counter = 0;
double tot = 0;
for (Tick t: TickQueue)
{
if ((t.getAskPrice() == 0 && t.getBidPrice() == 0) || (t.getAskPrice() == 0) || (t.getBidPrice() == 0))
{
tot += 0;
}
else
{
tot += (t.getAskPrice() - t.getBidPrice());
counter++;
}
}
double avg = tot/counter;
Run Code Online (Sandbox Code Playgroud)