2. Add Two Numbers

僅記錄

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
        int val=0;
        ListNode resalt= new ListNode(0);
        ListNode current = resalt;
        while(l1!=null||l2!=null||val>0){
            if(l1!=null){
                val+=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                val+=l2.val;
                l2=l2.next;
            }
            current.next= new ListNode(val%10);
            val = val/10;
            current  = current.next;
        }
        return resalt.next;
    }
}
上一篇