2. Add Two 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 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.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

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

# 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

        """

        node1, node2 = l1, l2

        newList = newNode = ListNode(0)

        carry = 0

        while node1 or node2 or carry:

            v1, v2 = 0, 0

            if node1:

                v1, node1 = node1.val, node1.next

            if node2:

                v2, node2 = node2.val, node2.next

            val = (v1 + v2 + carry)

            carry = 1 if val >= 10 else 0

            newNode.next = ListNode(val % 10)

            newNode = newNode.next

        return newList.next

来自为知笔记(Wiz)

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

时间: 2024-08-29 01:54:07

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

[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

[LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

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

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

You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 ->

leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)

You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 ->

[CareerCup] 18.1 Add Two Numbers 两数相加

18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我们实现两数相加,但是不能用加号或者其他什么数学运算符号,那么我们只能回归计算机运算的本质,位操作Bit Manipulation,我们在做加法运算的时候,每位相加之后可能会有进位Carry产生,然后在下一位计算时需要加上进位一起运算,那么我们能不能将两部分拆开呢,我们来看一个例子759+674 1.

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a

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

LeetCode Add Two Numbers 两个数相加

1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *creatnode(int a){ 12 ListNode *nod=new ListNode(a);

【LeetCode】Add Two Numbers(两数相加)

这道题是LeetCode里的第2到题. 这道题的条件判断很简单,如下: 1.是否为尾节点 2.是否产生进位 3.是否等于9 4.是否需要拓展空间 代码如下: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *p = l1, *q = l2; int add, carry = 0;//carry标志进位 while (1) { add = p->val + q->val + carry; p->val = add