|
| 1 | +// Definition of a singly‑linked list node |
| 2 | +// val – the numeric value stored in this node |
| 3 | +// next – pointer to the next node (or null if end of list) |
| 4 | +function ListNode(val, next) { |
| 5 | + this.val = val === undefined ? 0 : val; |
| 6 | + this.next = next === undefined ? null : next; |
| 7 | +} |
| 8 | + |
| 9 | +// addTwoNumbers takes two non‑empty linked lists l1 and l2, |
| 10 | +// where each node contains a single digit, and returns a |
| 11 | +// new linked list representing their sum, digit by digit. |
| 12 | +// Digits are stored in reverse order: [2→4→3] is 342. |
| 13 | +var addTwoNumbers = function (l1, l2) { |
| 14 | + // dummyHead is a placeholder that makes it easy to return the head later |
| 15 | + let dummyHead = new ListNode(0); |
| 16 | + let current = dummyHead; // moves along building the result list |
| 17 | + let carry = 0; // holds any “carry” digit between additions |
| 18 | + |
| 19 | + // Continue while either list has nodes left or there’s a carry to process |
| 20 | + while (l1 !== null || l2 !== null || carry !== 0) { |
| 21 | + // Extract the current digit values (or 0 if one list is shorter) |
| 22 | + let val1 = l1 ? l1.val : 0; |
| 23 | + let val2 = l2 ? l2.val : 0; |
| 24 | + |
| 25 | + // Sum the two digits plus any carry from the previous step |
| 26 | + let sum = val1 + val2 + carry; |
| 27 | + |
| 28 | + // carry is the tens place of sum (e.g. sum=15 → carry=1) |
| 29 | + carry = Math.floor(sum / 10); |
| 30 | + |
| 31 | + // The new digit is sum mod 10 (e.g. 15 % 10 = 5) |
| 32 | + current.next = new ListNode(sum % 10); |
| 33 | + |
| 34 | + // Advance pointers |
| 35 | + current = current.next; |
| 36 | + if (l1) l1 = l1.next; |
| 37 | + if (l2) l2 = l2.next; |
| 38 | + } |
| 39 | + |
| 40 | + // The real result starts at dummyHead.next (skip the placeholder) |
| 41 | + return dummyHead.next; |
| 42 | +}; |
| 43 | + |
| 44 | +// Helper: convert a JavaScript array into a linked list |
| 45 | +function arrayToList(arr) { |
| 46 | + let dummy = new ListNode(0); |
| 47 | + let curr = dummy; |
| 48 | + for (let x of arr) { |
| 49 | + curr.next = new ListNode(x); |
| 50 | + curr = curr.next; |
| 51 | + } |
| 52 | + return dummy.next; |
| 53 | +} |
| 54 | + |
| 55 | +// Helper: convert a linked list back into an array for easy logging |
| 56 | +function listToArray(node) { |
| 57 | + let out = []; |
| 58 | + while (node) { |
| 59 | + out.push(node.val); |
| 60 | + node = node.next; |
| 61 | + } |
| 62 | + return out; |
| 63 | +} |
| 64 | + |
| 65 | +// --- Example usage --- |
| 66 | + |
| 67 | +// Build two lists from arrays: |
| 68 | +// l1: [2,4,3] represents the number 342 |
| 69 | +// l2: [5,6,4] represents the number 465 |
| 70 | +const l1 = arrayToList([2, 4, 3]); |
| 71 | +const l2 = arrayToList([5, 6, 4]); |
| 72 | + |
| 73 | +// Compute their sum: 342 + 465 = 807, which should be [7,0,8] |
| 74 | +const resultList = addTwoNumbers(l1, l2); |
| 75 | + |
| 76 | +// Convert result back to array and print |
| 77 | +console.log(listToArray(resultList)); // Outputs: [7, 0, 8] |
| 78 | + |
| 79 | +// Time Complexity: O(max(n, m)) where n, m are the lengths of l1 and l2 |
| 80 | +// Space Complexity: O(max(n, m)) for the new result list |
0 commit comments