Scribbling

[Java 101] LeetCode: 2. Add Two Numbers 본문

Computer Science/Java

[Java 101] LeetCode: 2. Add Two Numbers

focalpoint 2023. 2. 14. 02:32
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */

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