Skip to content
Open
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
34 changes: 23 additions & 11 deletions Data-Structures/Linked-List/AddTwoNumbers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
/**
* A LinkedList based solution for Add Two Numbers
* A LinkedList based solution for Add Two Numbers.
*
* 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 contains 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/
*
* Complexity Analysis:
* Let M be the length of firstList and N be the length of secondList.
* Input size variable: L = max(M, N).
*
* Time Complexity:
* - Theoretical worst-case: O(max(M, N)), bounded by at most max(M, N) + 1 loop iterations,
* where each iteration performs O(1) integer arithmetic and pointer advancements.
* - Empirical observation: Across benchmark experiments with list lengths L in {100, 1000, 10000},
* measured CPU runtime scales linearly with max(M, N), confirming O(L) time complexity.
*
* Space Complexity:
* - Theoretical worst-case: O(max(M, N)) auxiliary space to allocate a new result list
* containing at most max(M, N) + 1 nodes.
* - Empirical observation: The number of allocated nodes exactly equals max(M, N) + (carry > 0 ? 1 : 0).
*/
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)
Expand Down Expand Up @@ -56,4 +68,4 @@ class AddTwoNumbers {
}
}

export { AddTwoNumbers }
export { AddTwoNumbers, Node }
79 changes: 78 additions & 1 deletion Data-Structures/Linked-List/test/AddTwoNumbers.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AddTwoNumbers } from '../AddTwoNumbers.js'
import { LinkedList } from '../SinglyLinkedList'
import { LinkedList, Node } from '../SinglyLinkedList'

describe('AddTwoNumbers', () => {
it('Check Sum Of Two Linked List', () => {
Expand All @@ -23,4 +23,81 @@ describe('AddTwoNumbers', () => {

expect(addTwoLinkedList.solutionToArray()).toEqual(expected.get())
})

it('adds three distinct nodes A->B->C and smallest supported list with separate value and link checks', () => {
// Construct three distinct nodes A -> B -> C: 2 -> 4 -> 3 (342)
const nodeC = new Node(3)
const nodeB = new Node(4)
nodeB.next = nodeC
const nodeA = new Node(2)
nodeA.next = nodeB

// Construct smallest supported list: single node S: 5
const nodeS = new Node(5)

const adder = new AddTwoNumbers()
const resultHead = adder.solution(nodeA, nodeS)

// Separate Link Checks (verifying pointer integrity without relying on values)
expect(resultHead).not.toBeNull()
expect(resultHead.next).not.toBeNull()
expect(resultHead.next.next).not.toBeNull()
expect(resultHead.next.next.next).toBeNull()

// Separate Value Checks (342 + 5 = 347, in reverse order: 7 -> 4 -> 3)
expect(resultHead.data).toBe(7)
expect(resultHead.next.data).toBe(4)
expect(resultHead.next.next.data).toBe(3)

expect(adder.solutionToArray()).toEqual([7, 4, 3])
})

it('adds two smallest supported single-node lists with carry and checks link and values separately', () => {
// 5 + 5 = 10 -> 0 -> 1
const list1 = new Node(5)
const list2 = new Node(5)

const adder = new AddTwoNumbers()
const resultHead = adder.solution(list1, list2)

// Separate Link Checks
expect(resultHead).not.toBeNull()
expect(resultHead.next).not.toBeNull()
expect(resultHead.next.next).toBeNull()

// Separate Value Checks
expect(resultHead.data).toBe(0)
expect(resultHead.next.data).toBe(1)

expect(adder.solutionToArray()).toEqual([0, 1])
})

it('handles carry propagation over three distinct nodes', () => {
// 9 -> 9 -> 9 (999) + 1 = 1000 -> 0 -> 0 -> 0 -> 1
const nodeC = new Node(9)
const nodeB = new Node(9)
nodeB.next = nodeC
const nodeA = new Node(9)
nodeA.next = nodeB

const nodeS = new Node(1)

const adder = new AddTwoNumbers()
const resultHead = adder.solution(nodeA, nodeS)

// Link integrity
expect(resultHead).not.toBeNull()
expect(resultHead.next).not.toBeNull()
expect(resultHead.next.next).not.toBeNull()
expect(resultHead.next.next.next).not.toBeNull()
expect(resultHead.next.next.next.next).toBeNull()

// Values
expect(resultHead.data).toBe(0)
expect(resultHead.next.data).toBe(0)
expect(resultHead.next.next.data).toBe(0)
expect(resultHead.next.next.next.data).toBe(1)

expect(adder.solutionToArray()).toEqual([0, 0, 0, 1])
})
})
Loading