I was doing following leetcode question:(我在做以下leetcode问题:)
You are given two non-empty linked lists representing two non-negative integers.(您将获得两个非空链表,它们代表两个非负整数。)
The digits are stored in reverse order and each of their nodes contain a single digit.(这些数字以相反的顺序存储,并且它们的每个节点都包含一个数字。) Add the two numbers and return it as a linked list.(将两个数字相加,并将其作为链表返回。)You may assume the two numbers do not contain any leading zero, except the number 0 itself.(您可能会假设两个数字除了数字0本身以外都不包含任何前导零。)
For which I stumbled upon following algo(为此我偶然发现了算法)
function ListNode(val) {
this.val = val;
this.next = null;
}
//This functiomn is called
var addTwoNumbers = function(l1, l2) {
let remainder = 0
let l3 = {}
let head = l3
while (l1 || l2 || remainder) {
let sum = remainder
function sumList (linkedList) {
if (linkedList) {
sum += linkedList.val
return linkedList.next
}
return null
}
l1 = sumList(l1)
l2 = sumList(l2)
if (sum>9) {
remainder = 1
sum -= 10
} else {
remainder = 0
}
head.next = new ListNode(sum)
head = head.next
}
return l3.next
};
Here, I am unable to comprehend the point of l3?(在这里,我无法理解l3的观点?)
If i remove it the algo fails?(如果我将其删除,算法会失败吗?)Question Link: https://leetcode.com/problems/add-two-numbers/(问题链接: https : //leetcode.com/problems/add-two-numbers/)
ask by anny123 translate from so