-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbersusinglinked.java
More file actions
38 lines (34 loc) · 1.13 KB
/
AddTwoNumbersusinglinked.java
File metadata and controls
38 lines (34 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class AddTwoNumbersusinglinked {
public static void main(String[] args) {
ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
// Calling the addTwoNumbers method and printing the result
ListNode result = addTwoNumbers(l1, l2);
printList(result);
}
public static void printList(ListNode head) {
while (head != null) {
System.out.print(head.val + " ");
head = head.next;
}
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode temp = new ListNode(0);
ListNode current = temp;
int carry = 0;
while (l1 != null || l2 != null || carry > 0) {
if (l1 != null) {
carry += l1.val;
l1 = l1.next;
}
if (l2 != null) {
carry += l2.val;
l2 = l2.next;
}
current.next = new ListNode(carry % 10);
carry /= 10;
current = current.next;
}
return temp.next;
}
}