mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
merge: Implement Add Two Numbers (Linked List) (#888)
* Implement Add Two Numbers (Linked List) * Implement Add Two Numbers (Linked List) * Implement Add Two Numbers (Linked List) Co-authored-by: Shakil Ahmmed <shakilahmmed@ShakilAhmmeds-MacBook-Pro.local>
This commit is contained in:
59
Data-Structures/Linked-List/AddTwoNumbers.js
Normal file
59
Data-Structures/Linked-List/AddTwoNumbers.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* 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 }
|
@ -213,4 +213,4 @@ class LinkedList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { LinkedList }
|
export { Node, LinkedList }
|
||||||
|
26
Data-Structures/Linked-List/test/AddTwoNumbers.test.js
Normal file
26
Data-Structures/Linked-List/test/AddTwoNumbers.test.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { AddTwoNumbers } from '../AddTwoNumbers.js'
|
||||||
|
import { LinkedList } from '../SinglyLinkedList'
|
||||||
|
|
||||||
|
describe('AddTwoNumbers', () => {
|
||||||
|
it('Check Sum Of Two Linked List', () => {
|
||||||
|
const list1 = new LinkedList()
|
||||||
|
list1.addFirst(2)
|
||||||
|
list1.addLast(4)
|
||||||
|
list1.addLast(3)
|
||||||
|
|
||||||
|
const list2 = new LinkedList()
|
||||||
|
list2.addFirst(5)
|
||||||
|
list2.addLast(6)
|
||||||
|
list2.addLast(4)
|
||||||
|
|
||||||
|
const expected = new LinkedList()
|
||||||
|
expected.addFirst(7)
|
||||||
|
expected.addLast(0)
|
||||||
|
expected.addLast(8)
|
||||||
|
|
||||||
|
const addTwoLinkedList = new AddTwoNumbers()
|
||||||
|
addTwoLinkedList.solution(list1.headNode, list2.headNode)
|
||||||
|
|
||||||
|
expect(addTwoLinkedList.solutionToArray()).toEqual(expected.get())
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user