445. Add Two Numbers II 两个数字相加2

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

# Definition for singly-linked list.

# class ListNode:

#     def __init__(self, x):

#         self.val = x

#         self.next = None

class Solution:

    def addTwoNumbers(self, l1, l2):

        """

        :type l1: ListNode

        :type l2: ListNode

        :rtype: ListNode

        """

        def list2arr(root):

            res = []

            while root:

                res.append(root.val)

                root = root.next

            return res

        arr1, arr2 = list2arr(l1), list2arr(l2)

        index1, index2 = len(arr1) - 1, len(arr2) - 1

        carry = 0

        root = None

        while index1 >= 0 or index2 >= 0 or carry:

            v1, v2 = 0, 0

            if index1 >= 0:

                v1 = arr1[index1]

                index1 -= 1

            if index2 >= 0:

                v2 = arr2[index2]

                index2 -= 1

            val = (v1 + v2 + carry)

            carry = 1 if val >= 10 else 0

            newNode = ListNode(val % 10)

            newNode.next = root

            root = newNode

        return root

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8445792.html

时间: 2024-10-10 03:06:33

445. Add Two Numbers II 两个数字相加2的相关文章

[LeetCode] Add Two Numbers II 两个数字相加之二

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

Leetcode刷题总结: 445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

【Leetcode】445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

445. Add Two Numbers II - Medium

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

445 Add Two Numbers II

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

LeetCode "445. Add Two Numbers II"

A natural stack based solution. Seriously, whey 'Medium'? /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(Lis

LeetCode 445. 两数相加 II(Add Two Numbers II)

445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进阶: 如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转. LeetCode445. Add Two Numbers II中等 示例: 输入: (7 -> 2 -> 4 -> 3) + (5 ->

[CareerCup] 2.5 Add Two Numbers 两个数字相加

2..5 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the su