附上通过代码:
/**
- Definition for singly-linked list.
- function ListNode(val, next) {
-
this.val = (val===undefined ? 0 : val)
-
this.next = (next===undefined ? null : next)
- }
/
/* - @param {ListNode} l1
- @param {ListNode} l2
- @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
let addOne=0
let sum=new ListNode(‘0’)
let head=sum
while(l1||l2||addOne){
let num1=l1!==null?l1.val:0
let num2=l2!==null?l2.val:0
let r1=num1+num2+addOne
addOne=r1>=10?1:0
sum.next=new ListNode(r1%10)
sum=sum.next
if(l1) l1=l1.next
if(l2) l2=l2.next
}
return head.next
};
此题总结:
1.对链表的熟悉度
2.let跟var的区别,百度的是,var是定义全局变量,let是定义局部变量
3.?:的便利性
4.return的位置别写错了!!!!!!!!