Files
Roland Hummel 86d333ee94 feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
2023-10-04 02:38:19 +05:30

60 lines
1.4 KiB
JavaScript

/**
* A LinkedList based solution for Add Two Numbers
*
*/
import { Node } from './SinglyLinkedList.js'
/*
Problem Statement:
Given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
Link for the Problem: https://leetcode.com/problems/add-two-numbers/
*/
class AddTwoNumbers {
constructor() {
this.dummyNode = new Node(0)
}
solution(firstList, secondList) {
let firstRunner = firstList
let secondRunner = secondList
let tail = this.dummyNode
let carry = 0
while (firstRunner != null || secondRunner != null) {
const firstNumber = firstRunner ? firstRunner.data : 0
const secondNumber = secondRunner ? secondRunner.data : 0
const sum = carry + firstNumber + secondNumber
carry = parseInt(sum / 10)
tail.next = new Node(sum % 10)
tail = tail.next
if (firstRunner) {
firstRunner = firstRunner.next
}
if (secondRunner) {
secondRunner = secondRunner.next
}
}
if (carry > 0) {
tail.next = new Node(carry % 10)
}
return this.dummyNode.next
}
solutionToArray() {
const list = []
let currentNode = this.dummyNode.next
while (currentNode) {
list.push(currentNode.data)
currentNode = currentNode.next
}
return list
}
}
export { AddTwoNumbers }