【leetcode445. 两数相加 II】比之前那个两数相加增加了点难度:
https://leetcode-cn.com/problems/add-two-numbers-ii
# Definition for singly-linked list.
# class ListNode:
#def __init__(self, x):
#self.val = x
#self.next = Noneclass Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
list1, list2 = [], []
while l1:
list1.append(l1.val)
l1 = l1.next
while l2:
list2.append(l2.val)
l2 = l2.next
res = ListNode(0)
flag = 0
cur = res
while list1 or list2:
x, y = 0, 0
if list1:
x = list1.pop()
if list2:
y = list2.pop()
sum_xy = flag + x + y
flag = sum_xy // 10
tmp = ListNode(sum_xy % 10)
tmp.next = cur.next
cur.next = tmp
if flag:
tmp = ListNode(flag)
tmp.next = cur.next
cur.next = tmp
return res.next
推荐阅读
- 数据结构与算法|【算法】力扣第 266场周赛
- leetcode|今天开始记录自己的力扣之路
- Python|Python 每日一练 二分查找 搜索旋转排序数组 详解
- 【LeetCode】28.实现strstr() (KMP超详细讲解,sunday解法等五种方法,java实现)
- LeetCode-35-搜索插入位置-C语言
- leetcode python28.实现strStr()35. 搜索插入位置
- Leetcode Permutation I & II
- python|leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
- LeetCode 28 Implement strStr() (C,C++,Java,Python)
- Python|Python Leetcode(665.非递减数列)