-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
64 lines (63 loc) · 1.69 KB
/
Copy pathsolution.java
File metadata and controls
64 lines (63 loc) · 1.69 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
54
55
56
57
58
59
60
61
62
63
64
// 2. Add Two Numbers
// https://leetcode.com/problems/add-two-numbers/
// Medium | Java | Accepted 2025-11-12
// Runtime 1 ms | Memory 46.3 MB
/**
* 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) {
int carry = 0;
int length = 0;
ListNode dummy = new ListNode(-1);
ListNode temp = dummy;
ListNode add1 = l1;
ListNode add2 = l2;
while(add1!=null && add2!=null)
{
int add = add1.val + add2.val + carry;
int val = add%10;
carry = (add-10) >= 0 ? 1 : 0;
temp.next = new ListNode(val);
temp = temp.next;
add1 = add1.next;
add2 = add2.next;
}
if(add1 == null)
{
while(add2!=null)
{
int add = add2.val + carry;
int val = add%10;
carry = (add-10) >= 0 ? 1 : 0;
temp.next = new ListNode(val);
temp = temp.next;
add2 = add2.next;
}
}
if(add2 == null)
{
while(add1!=null)
{
int add = add1.val + carry;
int val = add%10;
carry = (add-10) >= 0 ? 1 : 0;
temp.next = new ListNode(val);
temp = temp.next;
add1 = add1.next;
}
}
if(carry==1)
{
temp.next = new ListNode(1);
}
return dummy.next;
}
}