l3 is there to keep a record of all the values that have been changed for head.
(l3在那里保留所有已更改为head的值的记录。)
If I say that: (如果我这样说:)
var a = {};
var b = a;
Then b is not its own object.
(那么b不是它自己的对象。)
It's a pointer to the same exact object as a, so if you change a value in b, a updates to reflect that change as well. (它是与a相同的对象的指针,因此,如果您更改b中的值,则也会进行更新以反映该更改。)
That is still the case when you dive deeper into nested objects.
(当您深入嵌套对象时,情况仍然如此。)
If I say that: (如果我这样说:)
var c = {value: 1, next:{innerValue: 1}};
var d = c;
d.value = 2;
Then c updates to reflect the new value as well, as I've already explained.
(正如我已经解释的,然后c也会更新以反映新值。)
But the interesting part is that if I then go on to assign d to the nested object and edit the nested object, like so: (但是有趣的是,如果我继续将d分配给嵌套对象并编辑嵌套对象,如下所示:)
d = d.next;
d.innerValue = 2;
That still updates c!
(仍然更新c!)
So c is keeping a record of all of these updates, while d is free to dive deeper into its nested objects. (因此,c保留所有这些更新的记录,而d可以自由地更深入地研究其嵌套对象。)
After running this code, c and d will look like this: (运行此代码后,c和d将如下所示:)
console.log(c);//{value: 2, next:{innerValue: 2}}
console.log(d);//{innerValue: 2}
This is what l3 does.
(这就是l3所做的。)
l3 is to head as c is to d, so when the algorithm says: (l3朝向c朝向d,所以当算法说:)
head.next = new ListNode(sum)
head = head.next
...you don't have to worry about losing your history because l3 is keeping a record of everything.
(...您不必担心会丢失历史记录,因为l3会保留所有记录。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…