-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToNums.java
More file actions
53 lines (46 loc) · 1.76 KB
/
AddToNums.java
File metadata and controls
53 lines (46 loc) · 1.76 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Definition for singly-linked list.
* public 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 Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Initialize a new linked list to store the result
ListNode result = new ListNode(0);
ListNode curr = result;
int carry = 0;
// Iterate over both linked lists as long as they have nodes
while (l1 != null || l2 != null) {
// Get the values of the current nodes in the linked lists
// If a linked list has no more nodes, use 0 as the value
int x = (l1 != null) ? l1.val : 0;
int y = (l2 != null) ? l2.val : 0;
// Add the values and the carry from the previous iteration
int s = x + y + carry;
// Update the carry for the next iteration
carry = s / 10;
// Append the last digit of the sum to the result linked list
curr.next = new ListNode(s % 10);
curr = curr.next;
// Move to the next nodes in the linked lists
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
// If there is a carry left, append it to the result linked list
if (carry > 0) {
curr.next = new ListNode(carry);
}
// Return the result linked list, starting from the second node
// (the first node was only used as a placeholder)
return result.next;
}
}