update your solution
                        
                        
                        
                            class Solution {
                        
                                func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
                        
                                    let result = ListNode()
                        
                                    var resultTail = result
                        
                                    var carry: Int = 0
                        
                                    var out: Int = 0
                        
                                    var val1: Int = 0
                        
                                    var val2: Int = 0
                        
                            
                        
                                    var head1: ListNode? = l1
                        
                                    var head2: ListNode? = l2
                        
                            
                        
                                    while (l1 != nil || l2 != nil || carry != 0) {
                        
                            
                        
                                        val1 = head1 != nil ? head1!.val : 0
                        
                                        val2 = head2 != nil ? head2!.val : 0
                        
                            
                        
                                        out = (val1 + val2 + carry) % 10
                        
                                        carry = (val1 + val2 + carry) / 10
                        
                            
                        
                                        resultTail.next = ListNode(out)
                        
                                        resultTail = resultTail.next!
                        
                            
                        
                                        if let getHead1 = head1?.next {
                        
                                        
                        
                                        head1 = getHead1
                        
                                        } else {
                        
                                          
                        
                                            print("head1 next is nil")
                        
                                              break
                        
                                        }
                        
                                        if let getHead2 = head2?.next {
                        
                                                   
                        
                                                   head2 = getHead2
                        
                                                   } else {
                        
                                             
                        
                                                       print("head2 next is nil")
                        
                                             break
                        
                                                   }
                        
                                       
                        
                            
                        
                                    }
                        
                                    return result.next!
                        
                                }
                        
                            }
                        
                
             
            
                
                    
                        Just out of curiosity I've taken a look at what happens under the hood, and I've used [dtruss/strace][1] on each test.
                        
                        
                        
                        C++
                        
                        
                        
                            ./a.out < in
                        
                            Saw 6512403 lines in 8 seconds.  Crunch speed: 814050
                        
                        
                        
                        syscalls `sudo dtruss -c ./a.out < in`
                        
                        
                        
                            CALL                                        COUNT
                        
                            __mac_syscall                                   1
                        
                            <snip>
                        
                            open                                            6
                        
                            pread                                           8
                        
                            mprotect                                       17
                        
                            mmap                                           22
                        
                            stat64                                         30
                        
                            read_nocancel                               25958
                        
                        
                        
                        
                        
                        Python
                        
                        
                        
                            ./a.py < in
                        
                            Read 6512402 lines in 1 seconds. LPS: 6512402
                        
                        
                        
                        syscalls `sudo dtruss -c ./a.py < in`
                        
                        
                        
                            CALL                                        COUNT
                        
                            __mac_syscall                                   1
                        
                            <snip>
                        
                            open                                            5
                        
                            pread                                           8
                        
                            mprotect                                       17
                        
                            mmap                                           21
                        
                            stat64                                         29
                        
                        
                        
                          [1]: http://en.wikipedia.org/wiki/Strace