Function
dfs
(l1, l2, tail, sentinel, carry)
Source from the content-addressed store, hash-verified
| 30 | }; |
| 31 | |
| 32 | const dfs = (l1, l2, tail, sentinel, carry) => { |
| 33 | const sum = (l1?.val || 0) + (l2?.val || 0) + carry; |
| 34 | const val = sum % 10; |
| 35 | carry = Math.floor(sum / 10); |
| 36 | |
| 37 | tail.next = new ListNode(val); |
| 38 | tail = tail.next; |
| 39 | |
| 40 | l1 = l1?.next || null; |
| 41 | l2 = l2?.next || null; |
| 42 | |
| 43 | add( |
| 44 | l1, |
| 45 | l2, |
| 46 | tail, |
| 47 | sentinel, |
| 48 | carry, |
| 49 | ); /* Time O(MAX(N, M)) | Space O(MAX(N, M)) */ |
| 50 | |
| 51 | return sentinel.next; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * https://leetcode.com/problems/add-two-numbers/ |
Tested by
no test coverage detected