Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Non-formatted] [Non-tested] 2. Add Two Numbers#123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
axross wants to merge1 commit intomaster
base:master
Choose a base branch
Loading
from2
Draft
Show file tree
Hide file tree
Changes fromall 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 deletionssolutions/add_two_numbers.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
import { SinglyLinkedListNode } from '../types/linked_list.ts';
Copy link
OwnerAuthor

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
OwnerAuthor

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)returnnodeA??nodeB??null;
if(!nodeA||!nodeB){returnnodeA??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 deletionssolutions/add_two_numbers_test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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
OwnerAuthor

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]));
});

[8]ページ先頭

©2009-2025 Movatter.jp