-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.AddTwoNumbers.rb
47 lines (46 loc) · 1.02 KB
/
2.AddTwoNumbers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def add_two_numbers(l1, l2)
carry = 0
dummy = ListNode.new(0)
start = dummy
while l1 != nil && l2 != nil
sum = l1.val + l2.val + carry
val = sum % 10
carry = sum / 10
start.next = ListNode.new(val)
start = start.next
l1 = l1.next
l2 = l2.next
end
while l1!= nil
sum = l1.val+ carry
val = sum % 10
carry = sum / 10
start.next = ListNode.new(val)
start = start.next
l1 = l1.next
end
while l2!= nil
sum = l2.val+ carry
val = sum % 10
val = sum % 10
carry = sum / 10
start.next = ListNode.new(val)
start = start.next
l2 = l2.next
end
if carry == 1
start.next = ListNode.new(1)
end
dummy.next
end