1 class Solution {
2
3 /**
4 * @param ListNode $l1
5 * @param ListNode $l2
6 * @return ListNode
7 */
8 function addTwoNumbers($l1, $l2) {
9 $tmp = [];
10 $flag = 0;
11 while($flag>0 || $l1->val !== null || $l2->val !== null){
12 $num1 = ($l1&&$l1->val)?$l1->val:0;
13 $num2 = ($l2&&$l2->val)?$l2->val:0;
14 $num = $num1+$num2+$flag;
15 $flag = 0;
16 if($num>9){
17 $flag = 1;
18 }
19 $tmp[] = $num%10;
20 $l1 = $l1->next;
21 $l2 = $l2->next;
22 }
23 $tmp = array_reverse($tmp);
24 $res = new ListNode($tmp[0]);
25 $res->next = null;
26 unset($tmp[0]);
27 foreach($tmp as $v){
28 $p = new ListNode($v);
29 $p->next = $res;
30 $res = $p;
31 }
32 return $res;
33 }
34 }
![](https://img2018.cnblogs.com/blog/1366775/201903/1366775-20190319192644217-1988118816.png)