https://leetcode.com/problems/add-two-numbers/

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1 == null) return l2;
        if(l2 == null) return l1;
    
        ListNode head = new ListNode(0);
        ListNode p = head;
        
        int tmp = 0;
        while(l1!=null || l2!=null || tmp!=0) {
            if(l1!=null){
                tmp += l1.val;
                l1 = l1.next;
            }
            if(l2!=null){
                tmp += l2.val;
                l2 = l2.next;
            }
            p.next = new ListNode(tmp%10);
            p = p.next;
            tmp = tmp/10;
        }
        return head.next;
    }
}

相关文章:

  • 2021-11-29
  • 2022-01-23
  • 2022-02-05
  • 2021-12-20
  • 2021-08-09
  • 2021-07-28
  • 2022-02-26
猜你喜欢
  • 2021-12-03
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2021-03-31
  • 2021-09-09
相关资源
相似解决方案