Created
June 1, 2020 16:28
-
-
Save davidseek/34fc2bfffc2fe863620369af5d1c1278 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
| func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { | |
| // Get a mutable reference to each head of l1 and l2 | |
| var currentL1: ListNode? = l1 | |
| var currentL2: ListNode? = l2 | |
| // Create a reference to a node we're going to append on | |
| var sumList: ListNode? = ListNode(-1) | |
| // And a reference to its head | |
| var head: ListNode? = sumList | |
| // The carry for values > 9 | |
| var carry: Int = 0 | |
| // While we haven't fully iterated both lists yet... | |
| while currentL1 != nil || currentL2 != nil { | |
| /** | |
| Get a dummy value of 0 for each list | |
| in the niche case that either list | |
| is longer than the other. | |
| */ | |
| var value1: Int = 0 | |
| var value2: Int = 0 | |
| // Replace with l1's value if it exists | |
| if let l1 = currentL1 { | |
| value1 = l1.val | |
| } | |
| // Same for l2 | |
| if let l2 = currentL2 { | |
| value2 = l2.val | |
| } | |
| // Sum up both values and the last carry | |
| let sum: Int = value1 + value2 + carry | |
| // Get the value < 10 | |
| let value = sum % 10 | |
| // Get the carry if above 10 | |
| carry = sum / 10 | |
| // Add a node with the new value | |
| sumList?.next = ListNode(value) | |
| // And move the head reference | |
| sumList = sumList?.next | |
| // Move l1 and l2 along as well | |
| currentL1 = currentL1?.next | |
| currentL2 = currentL2?.next | |
| } | |
| // If we still have carry left | |
| if carry > 0 { | |
| /** | |
| Add a node for the carry. | |
| This case could happen on | |
| (9 -> 9) + (1) = (0 -> 0 -> 1) | |
| */ | |
| sumList?.next = ListNode(carry) | |
| } | |
| // Return head.next to lose the -1 dummy head | |
| return head?.next | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment