-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAddTwoNumbers.java
67 lines (52 loc) · 2.08 KB
/
AddTwoNumbers.java
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
65
66
67
/**
* 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; }
* }
*/
/*
Runtime: 2 ms, faster than 99.46% of Java online submissions for Add Two Numbers.
Memory Usage: 42.5 MB, less than 89.13% of Java online submissions for Add Two Numbers.
*/
// Optimized Solution TC: O(n) SC: O(1)
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Taking Pointers To Start Of Both The Lists
ListNode l1Pointer = l1;
ListNode l2Pointer = l2;
// Creating A New Node For Storing The Sum
ListNode head = new ListNode(0);
// Creating Pointer To The head
ListNode currentNode = head;
int carry = 0;
// Iterating Till All 3 Conditions Satisfy
while(l1Pointer != null || l2Pointer != null || carry !=0){
// Taking Values As Input Till List Hits Null And If The Loop Still Continues To Run THen We Take 0 as Input
int l1Value = (l1Pointer != null) ? l1Pointer.val : 0;
int l2Value = (l2Pointer != null) ? l2Pointer.val : 0;
// Calculating Sum
int sum = l1Value + l2Value + carry;
// Calculating Last Digit
int lastDigit = sum%10;
// Calculating Carry
carry = sum/10;
// Storing Last Digit In A Node And Adding It To The Answer
ListNode lastDigitNode = new ListNode(lastDigit);
currentNode.next = lastDigitNode;
currentNode = currentNode.next;
// Base Iterations
if(l1Pointer != null){
l1Pointer = l1Pointer.next;
}
if(l2Pointer != null){
l2Pointer = l2Pointer.next;
}
}
// Returning The Final Result
return head.next;
}
}