8000 Added task 2 · jscrdev/LeetCode-in-TypeScript@8bed05e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bed05e

Browse files
authored
Added task 2
1 parent a8cbe8b commit 8bed05e

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

.github/workflows/node.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ jobs:
2828
if: ${{ needs.preCheck.outputs.isExecute == 'true' }}
2929
runs-on: ubuntu-latest
3030
steps:
31-
- name: Checkout code
32-
uses: actions/checkout@v2
31+
- uses: actions/checkout@v3
32+
with:
33+
fetch-depth: 0
3334

3435
- name: Commit records
3536
run: |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
- The number of nodes in each linked list is in the range `[1, 100]`.
34+
- `0 <= Node.val <= 9`
35+
- It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ListNode {
2+
public val: number | null;
3+
public next: ListNode | null;
4+
5+
constructor(val?: number | null, next?: ListNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.next = next === undefined ? null : next;
8+
}
9+
}
10+
11+
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
12+
const result = new ListNode(null);
13+
let next = result;
14+
let carryNum = 0;
15+
let currSum = 0;
16+
let num1 = 0;
17+
let num2 = 0;
18+
19+
while (l1 !== null || l2 !== null) {
20+
// @ts-ignore
21+
num1 = l1 !== null ? l1.val : 0;
22+
// @ts-ignore
23+
num2 = l2 !== null ? l2.val : 0;
24+
currSum = (num1 + num2 + carryNum) % 10;
25+
carryNum = Math.floor((num1 + num2 + carryNum) / 10);
26+
next.next = new ListNode(currSum);
27+
next = next.next;
28+
l1 = l1 !== null ? l1.next : null;
29+
l2 = l2 !== null ? l2.next : null;
30+
}
31+
if (carryNum) {
32+
next.next = new ListNode(carryNum);
33+
}
34+
return result.next;
35+
}
36+
37+
export { ListNode, addTwoNumbers };
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`add_two_numbers > should be worked well 1`] = `
4+
ListNode {
5+
"next": ListNode {
6+
"next": ListNode {
7+
"next": ListNode {
8+
"next": null,
9+
"val": 1,
10+
},
11+
"val": 3,
12+
},
13+
"val": 0,
14+
},
15+
"val": 2,
16+
}
17+
`;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// tslint:disable:no-magic-numbers
2+
import { addTwoNumbers, ListNode } from 'src/main/ts/g0001_0100/s0002_add_two_numbers/solution';
3+
import { describe, expect, test } from 'vitest';
4+
5+
const l11 = new ListNode(6, null);
6+
const l12 = new ListNode(5, l11);
7+
const l13 = new ListNode(1, l12);
8+
9+
describe('add_two_numbers', () => {
10+
test('should be worked well', () => {
11+
expect(addTwoNumbers(l13, l13)).toMatchSnapshot();
12+
});
13+
});

0 commit comments

Comments
 (0)
0