- Notifications
You must be signed in to change notification settings - Fork14
[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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||
import { SinglyLinkedListNode } from '../types/linked_list.ts'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
| ||||||
// 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
| ||||||
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; | ||||||
} |
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'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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])); | ||||||
}); |