You 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.
# 将int构建成listNode # reverse the digits of n and convert it to ListNode defbuildListNode(n): # 将int型倒序转成string n_str_reversed = str(n)[::-1] # 构建ListNode,且head[0]=p[0]=0 head = p = ListNode(0) for i in range(len(n_str_reversed)): # 按照顺序将n_str_reversed添加到链表p上 p.next = ListNode(int(n_str_reversed[i])) # 链表链增加 p = p.next # 因为head[0]=0 所以从head[1]开始 return head.next
n = getNumber(l1) + getNumber(l2) return buildListNode(n)