Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions solutions/add_two_numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SinglyLinkedListNode } from '../types/linked_list.ts';
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Strings must use doublequote.

Suggested change
import { SinglyLinkedListNode } from '../types/linked_list.ts';
import { SinglyLinkedListNode } from "../types/linked_list.ts";


// 2. Add Two Numbers
// https://leetcode.com/problems/add-two-numbers/
export default function addTwoNumbers(nodeA: SinglyLinkedListNode<number>, nodeB: SinglyLinkedListNode<number>) {
if (!nodeA || !nodeB) return nodeA ?? nodeB ?? null;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
if (!nodeA || !nodeB) return nodeA ?? nodeB ?? null;
if (!nodeA || !nodeB) {return nodeA ?? nodeB ?? null;}


const sum = nodeA.val + nodeB.val;
const node = { val: sum % 10, next: null };
node.next = addTwoNumbers(nodeA.next, nodeB.next);

if (sum >= 10) {
node.next = addTwoNumbers(node.next, { val: 1, next: null });
}

return node;
}
10 changes: 10 additions & 0 deletions solutions/add_two_numbers_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test } from "https://deno.land/std/testing/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { createSinglyLinkedListNode } from '../test_utilities/linked_list.ts';
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Strings must use doublequote.

Suggested change
import { createSinglyLinkedListNode } from '../test_utilities/linked_list.ts';
import { createSinglyLinkedListNode } from "../test_utilities/linked_list.ts";

import addTwoNumbers from "./add_two_numbers.ts";

test("2. Add Two Numbers", () => {
assertEquals(addTwoNumbers(createSinglyLinkedListNode([2,4,3]), createSinglyLinkedListNode([5,6,4])), createSinglyLinkedListNode([7,0,8]));
assertEquals(addTwoNumbers(createSinglyLinkedListNode([9,9,9]), createSinglyLinkedListNode([1])), createSinglyLinkedListNode([0,0,0,1]));
assertEquals(addTwoNumbers(createSinglyLinkedListNode([1]), createSinglyLinkedListNode([1])), createSinglyLinkedListNode([2]));
});