@@ -32,4 +32,102 @@ You may assume the two numbers do not contain any leading zero, except the numbe
3232
3333* The number of nodes in each linked list is in the range ` [1, 100] ` .
3434* ` 0 <= Node.val <= 9 `
35- * It is guaranteed that the list represents a number that does not have leading zeros.
35+ * It is guaranteed that the list represents a number that does not have leading zeros.
36+
37+ To solve the Add Two Numbers problem in Java using a ` Solution ` class, we'll follow these steps:
38+
39+ 1 . Define a ` ListNode ` class to represent nodes in a linked list.
40+ 2 . Define a ` Solution ` class with a method named ` addTwoNumbers ` .
41+ 3 . Inside the ` addTwoNumbers ` method, traverse both input linked lists simultaneously:
42+ - Keep track of a carry variable to handle cases where the sum of two digits exceeds 9.
43+ - Calculate the sum of the current nodes' values along with the carry.
44+ - Update the carry for the next iteration.
45+ - Create a new node with the sum % 10 and attach it to the result linked list.
46+ - Move to the next nodes in both input lists.
47+ 4 . After finishing the traversal, check if there is any remaining carry. If so, add a new node with the carry to the result.
48+ 5 . Return the head of the result linked list.
49+
50+ Here's the implementation:
51+
52+ ``` java
53+ class ListNode {
54+ int val;
55+ ListNode next;
56+
57+ ListNode () {}
58+
59+ ListNode (int val ) {
60+ this . val = val;
61+ }
62+
63+ ListNode (int val , ListNode next ) {
64+ this . val = val;
65+ this . next = next;
66+ }
67+ }
68+
69+ public class Solution {
70+
71+ public ListNode addTwoNumbers (ListNode l1 , ListNode l2 ) {
72+ ListNode dummyHead = new ListNode ();
73+ ListNode curr = dummyHead;
74+ int carry = 0 ;
75+
76+ while (l1 != null || l2 != null ) {
77+ int sum = carry;
78+ if (l1 != null ) {
79+ sum += l1. val;
80+ l1 = l1. next;
81+ }
82+ if (l2 != null ) {
83+ sum += l2. val;
84+ l2 = l2. next;
85+ }
86+ curr. next = new ListNode (sum % 10 );
87+ curr = curr. next;
88+ carry = sum / 10 ;
89+ }
90+
91+ if (carry > 0 ) {
92+ curr. next = new ListNode (carry);
93+ }
94+
95+ return dummyHead. next;
96+ }
97+
98+ // Helper method to print a linked list
99+ public void printList (ListNode head ) {
100+ ListNode curr = head;
101+ while (curr != null ) {
102+ System . out. print(curr. val + " " );
103+ curr = curr. next;
104+ }
105+ System . out. println();
106+ }
107+
108+ public static void main (String [] args ) {
109+ Solution solution = new Solution ();
110+
111+ // Test cases
112+ ListNode l1 = new ListNode (2 , new ListNode (4 , new ListNode (3 )));
113+ ListNode l2 = new ListNode (5 , new ListNode (6 , new ListNode (4 )));
114+ ListNode result1 = solution. addTwoNumbers(l1, l2);
115+ System . out. print(" Example 1 Output: " );
116+ solution. printList(result1);
117+
118+ ListNode l3 = new ListNode (0 );
119+ ListNode l4 = new ListNode (0 );
120+ ListNode result2 = solution. addTwoNumbers(l3, l4);
121+ System . out. print(" Example 2 Output: " );
122+ solution. printList(result2);
123+
124+ ListNode l5 = new ListNode (9 , new ListNode (9 , new ListNode (9 , new ListNode (9 , new ListNode (9 , new ListNode (9 , new ListNode (9 )))))));
125+ ListNode l6 = new ListNode (9 , new ListNode (9 , new ListNode (9 , new ListNode (9 ))));
126+ ListNode result3 = solution. addTwoNumbers(l5, l6);
127+ System . out. print(" Example 3 Output: " );
128+ solution. printList(result3);
129+ }
130+ }
131+ ```
132+
133+ This implementation provides a solution to the Add Two Numbers problem using linked lists in Java.
0 commit comments