216人阅读 评论(0)

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):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
p = 0
result = None
current = None
while True:
addValue = l1.val + l2.val + p
p = 0

p = 1

if result == None:
result = node
current = node
else:
current.next = node
current = node

l1 = l1.next
l2 = l2.next

if l1 == None or l2 == None:
break

if l1 == None and l2 == None:
pass
elif l1 == None or l2 == None:
rest = None
if l1 == None:
rest = l2
else :
rest = l1

while True:
p = 0

p = 1

current.next = node
current = node

rest = rest.next
if rest == None:
break

if p == 0:
return result
else:
node = ListNode(p)
current.next = node
return result

0
0

个人资料
• 访问：122075次
• 积分：2649
• 等级：
• 排名：第16266名
• 原创：136篇
• 转载：56篇
• 译文：0篇
• 评论：36条
博客专栏
 Spring Boot Demos 文章：0篇 阅读：0