Skip to content

Commit d4d8af3

Browse files
committed
[feat] Added AddTwoNumberLinkedLists solution
1 parent 25e1c9b commit d4d8af3

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
12+
/**
13+
* Input: l1 = [2,4,3], l2 = [5,6,4]
14+
* Output: [7,0,8]
15+
* Explanation: 342 + 465 = 807.
16+
*/
17+
18+
/**
19+
* Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1]
20+
*/
21+
22+
class Solution {
23+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
24+
ListNode resultList = new ListNode();
25+
ListNode root = resultList;
26+
int carryOver = 0;
27+
int sum = 0;
28+
while (l1 != null && l2 != null) {
29+
sum = (l1.val + l2.val + carryOver) % 10;
30+
carryOver = (l1.val + l2.val + carryOver) / 10;
31+
ListNode temp = new ListNode(sum);
32+
root.next = temp;
33+
root = root.next;
34+
l1 = l1.next;
35+
l2 = l2.next;
36+
}
37+
38+
while (l1 != null) {
39+
sum = (l1.val + carryOver) % 10;
40+
carryOver = (l1.val + carryOver) / 10;
41+
ListNode temp = new ListNode(sum);
42+
root.next = temp;
43+
root = root.next;
44+
l1 = l1.next;
45+
}
46+
47+
while (l2 != null) {
48+
sum = (l2.val + carryOver) % 10;
49+
carryOver = (l2.val + carryOver) / 10;
50+
ListNode temp = new ListNode(sum);
51+
root.next = temp;
52+
root = root.next;
53+
l2 = l2.next;
54+
}
55+
56+
if (carryOver > 0) {
57+
ListNode temp = new ListNode(carryOver);
58+
root.next = temp;
59+
root = root.next;
60+
}
61+
62+
return resultList.next;
63+
}
64+
}

platform-problems/leetcode/may-2020-leetcoding-challenge/leetcode_18_permutation_as_substring/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public boolean checkAllValues(HashMap<Character, Integer> map, char[] arr, int i
2222
}
2323

2424
public boolean checkInclusion(String s1, String s2) {
25-
HashMap<Character,Integer> charMap = new HashMap<Character, Integer>();
25+
HashMap<Character,Integer> charMap = new HashMap<>();
2626
for (char c : s1.toCharArray()) {
2727
if (charMap.containsKey(c)) {
2828
int value = charMap.get(c);
@@ -35,7 +35,7 @@ public boolean checkInclusion(String s1, String s2) {
3535
char[] arr = s2.toCharArray();
3636
for (int i = 0; i < arr.length; i++) {
3737
if (charMap.containsKey(arr[i])) {
38-
HashMap<Character, Integer> cloneCharMap = (HashMap)charMap.clone();
38+
HashMap<Character, Integer> cloneCharMap = (HashMap<Character,Integer>)charMap.clone();
3939
if (checkAllValues(cloneCharMap, arr, i)) {
4040
return true;
4141
};

0 commit comments

Comments
 (0)