(LeetCode) T2. Add Two Numbers

ProblemYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.


Solve:



Thought :(为了方便,以后此项均为中文)主要是考虑进位问题,要判断l1和l2加完后,结果是否有进位,有的话要新建结点补上进位。

设置了两个变量判断l1和l2是否读完,注意当l1读完后会跳转到l2上,根据链表的性质,此时仍使用l1代替l2即可。