内容:
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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
大致意思:
存在将数值倒着写的列表,将列表相加相等于数值相加
参考答案一:
# 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):
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1+v2+carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
参考答案二:
# 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
"""
ret = ListNode(0)
cur = ret
add = 0
while l1 or l2 or add:
val = (l1.val if l1 else 0) + (l2.val if l2 else 0) + add
add = val / 10
cur.next = ListNode(val % 10)
cur = cur.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return ret.next
思路:
暂无
知识点:
1. 顺序赋值 a = b = 1
2. 方法 divmod() 获得余数和商
网友评论