Created
March 31, 2021 04:12
-
-
Save overcyn/a9549b7e576d64d578f92559bacb29ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var addTwoNumbers2 = function(l1, l2, carry) { | |
| var sum = carry | |
| var hasNext = carry != 0 | |
| var l1Next = null | |
| var l2Next = null | |
| if (l1 != null) { | |
| sum += l1.val | |
| hasNext = true | |
| l1Next = l1.next | |
| } | |
| if (l2 != null) { | |
| sum += l2.val | |
| hasNext = true | |
| l2Next = l2.next | |
| } | |
| var remainder = sum % 10 | |
| var carry = (sum - remainder) / 10 | |
| var next = null | |
| if (hasNext) { | |
| next = addTwoNumbers2(l1Next, l2Next, carry) | |
| } | |
| return ListNode(sum % 10, next) | |
| } | |
| var addTwoNumbers = function(l1, l2) { | |
| return addTwoNumbers2(l1, l2, 0) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment