12/9 注意一下循环终止条件不光是遍历到两个链表尾部,还要进位为0 class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode bef = new ListNode(); ListNode head = bef; int flag = 0; int nowval; while (true) { if (l1 == null && l2 == null) { if (flag == 0) { break; } else { nowval = flag; } } else if (l1 == null) { nowval = l2.val + flag; l2 = l2.next; } else if (l2 == null) { nowval = l1.val + flag; l1 = l1.next; } else { nowval = l1.val + l2.val + flag; l1 = l1.next; l2 = l2.next; } flag = nowval / 10; bef.next = new ListNode(nowval % 10); bef = bef.next; } return head.next; } }