package com.zerroi.leetcode.Three23; public class AddTwoNumbers { public static void main(String[] args) { ListNode l1 = new ListNode(2, new ListNode(2, new ListNode(3))); ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4))); SolutionFirst solutionFirst = new SolutionFirst(); ListNode listNode = solutionFirst.addTwoNumbers(l1, l2); } } 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 SolutionFirst { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode head = new ListNode(-1); ListNode cur = head; int carry = 0; int a = 0, b = 0; int value = 0; while (l1 != null || l2 != null) { if (l1 == null) { a = 0; } else { a = l1.val; } if (l2 == null) { b = 0; } else { b = l2.val; } value = a + b + carry; carry = value / 10; value = value % 10; cur.next = new ListNode(value); cur = cur.next; if (l1 != null) { l1 = l1.next; } if (l2 != null) { l2 = l2.next; } if (carry != 0) { cur.next = new ListNode(1); } } return cur; } }