Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- // 2. Add Two Numbers
- public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
- if (l1 == null) return l2;
- if (l2 == null) return l1;
- ListNode dummy = new ListNode(0);
- ListNode l3 = dummy;
- int sum = 0;
- while (l1 != null || l2 != null || sum != 0) {
- if (l1 != null) {
- sum += l1.val;
- l1 = l1.next;
- }
- if (l2 != null) {
- sum += l2.val;
- l2 = l2.next;
- }
- l3.next = new ListNode(sum % 10);
- l3 = l3.next;
- sum /= 10;
- }
- return dummy.next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement