题目描述
https://leetcode.com/problems/add-two-numbers/
解决方法
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
l1_str = ""
l2_str = ""
while l1:
l1_str += str(l1.val)
l1 = l1.next
while l2:
l2_str += str(l2.val)
l2 = l2.next
int_l1 = int(l1_str[::-1])
int_l2 = int(l2_str[::-1])
l3_str = str(int_l1 + int_l2)
node = None
i = 0
while i < len(l3_str):
l3 = ListNode(l3_str[i])
l3.next = node
node = l3
i = i+1
return l3
注意我这里运算出来的结果是l3_str 正序的,所以在l3是倒序指向