s1
and s2
.l1
and push all its elements onto s1
.l2
and push all its elements onto s2
.carry
to 0 and ans
to a new ListNode with value 0.s1
or s2
is not empty or carry
is not 0:
a. Pop elements from s1
and s2
if they are not empty and add them to carry
.
b. The new digit is carry % 10
and the new carry is carry / 10
.
c. Create a new node with the new digit and set it as the next of ans
.
d. Update ans
to this new node.