题目描述 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.
# 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
"""
l = ListNode(0)
p = l
flag=0
while l1 and l2:
summ = l1.val + l2.val + flag
new = ListNode(summ%10)
l.next = new
l = l.next
flag = summ//10
l1, l2 = l1.next, l2.next
while l1 != None:
summ = l1.val+flag
new = ListNode(summ%10)
flag = summ//10
l.next = new
l = l.next
l1 = l1.next
while l2 != None:
summ = l2.val+flag
new = ListNode(summ%10)
flag = summ//10
l.next = new
l = l.next
l2 = l2.next
if flag == 1:
new = ListNode(1)
l.next = new
return p.next
21. Merge Two Sorted Lists
题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
2 add 2 numbers题目描述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 tw...