def add_two_numbers(l1, l2) p, q, curr = l1, l2, dummy_head = ListNode.new(0) carry = 0 until p.nil? || q.nil? do x = p.nil? ? 0 : p.val y = q.nil? ? 0 : q.val sum = carry + x + y carry = sum / 10 curr.next = ListNode.new(sum % 10) curr = curr.next p = p.next unless p.nil? q = q.next unless q.nil? end curr.next = ListNode.new(carry) if carry > 0 dummy_head.next end
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.