Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. class Solution {
  2. // 2. Add Two Numbers
  3. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  4. if (l1 == null) return l2;
  5. if (l2 == null) return l1;
  6.  
  7. ListNode dummy = new ListNode(0);
  8. ListNode l3 = dummy;
  9. int sum = 0;
  10.  
  11. while (l1 != null || l2 != null || sum != 0) {
  12. if (l1 != null) {
  13. sum += l1.val;
  14. l1 = l1.next;
  15. }
  16. if (l2 != null) {
  17. sum += l2.val;
  18. l2 = l2.next;
  19. }
  20.  
  21. l3.next = new ListNode(sum % 10);
  22. l3 = l3.next;
  23. sum /= 10;
  24. }
  25. return dummy.next;
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement