龙空技术网

leetcode2. 两数相加-c语言-python3

篮球鉴赏家小华 53

前言:

今天我们对“python列表内的数字相加”都比较着重,兄弟们都需要知道一些“python列表内的数字相加”的相关资讯。那么小编也在网摘上搜集了一些关于“python列表内的数字相加””的相关资讯,希望我们能喜欢,大家一起来学习一下吧!

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例 1:

输入:l1 = [2,4,3], l2 = [5,6,4]

输出:[7,0,8]

解释:342 + 465 = 807.

示例 2:

输入:l1 = [0], l2 = [0]

输出:[0]

示例 3:

输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]

输出:[8,9,9,9,0,0,0,1]

提示:

每个链表中的节点数在范围 [1, 100] 内

0 <= Node.val <= 9

题目数据保证列表表示的数字不含前导零

c语言解题:

知识点:单链表的建立,无head,尾插入。

#include<stdio.h>#include<stdlib.h>//Definition for singly-linked list.struct ListNode {     int val;     struct ListNode *next; }; struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){    int carry = 0;    struct ListNode* ret = NULL, *tail = NULL;    struct ListNode* l1_head = l1,*l2_head = l2;    if (l1_head == NULL && l2_head== NULL)    {        return  ret = NULL;    }        while (l1_head != NULL || l2_head  != NULL)    {        struct ListNode* new_node = (struct ListNode*)malloc(sizeof(struct ListNode));        new_node->next = NULL;        int l1_head_val = l1_head == NULL ? 0 : l1_head->val;        int l2_head_val = l2_head == NULL ? 0 : l2_head->val;        new_node -> val = l1_head_val + l2_head_val + carry;        carry = (new_node -> val ) / 10 ;        new_node -> val =(new_node -> val ) % 10 ;         if(l1_head != NULL)         l1_head = l1_head ->next;         if(l2_head != NULL)         l2_head = l2_head ->next;         if(ret == NULL)         ret = new_node;         else         {            tail->next = new_node;         }          tail = new_node;    }         if (carry)         {            struct ListNode* new_node = (struct ListNode*)malloc(sizeof(struct ListNode));            new_node->next = NULL;            new_node -> val = carry;            if(ret == NULL)            ret = new_node;            else            {                tail->next = new_node;            }            tail = new_node;         }    return ret;    }int main(){    int l1arr[] = {9, 9, 9, 9, 9, 9, 9};    int l2arr[] = {9, 9, 9, 9};    struct ListNode*  l1, *l1tail, *p1;    struct ListNode* l2, * l2tail, *p2;    struct ListNode* ret;    int l1arrSize = sizeof(l1arr) / sizeof(int);    int l2arrSize = sizeof(l2arr) / sizeof(int);    l1tail = p1 = (struct ListNode*)malloc(sizeof(struct ListNode));    l2tail = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));    l1 = NULL;    l2 = NULL;    for (int i = 0; i < l1arrSize; i++)    {        p1 ->val = l1arr[i];        p1 ->next = NULL;        if(l1 == NULL)             l1 = p1;        else        {            l1tail->next  = p1;        }        l1tail = p1;        p1 = (struct ListNode*)malloc(sizeof(struct ListNode));    }    l1tail = NULL,p1 = NULL;     for (int i = 0; i < l2arrSize; i++)    {        p2 ->val = l2arr[i];        p2 ->next = NULL;        if(l2 == NULL)             l2 = p2;        else        {            l2tail->next  = p2;        }        l2tail = p2;        p2 = (struct ListNode*)malloc(sizeof(struct ListNode));    }    l2tail = NULL,p2 = NULL;    ret = addTwoNumbers(l1, l2);    printf("[%d,%d,%d,%d,%d,%d,%d,%d]",ret->val,ret->next->val,ret->next->next->val    ,ret->next->next->next->val,ret->next->next->next->next->val,ret->next->next->next->next->next->val    ,ret->next->next->next->next->next->next->val,ret->next->next->next->next->next->next->next->val);    return 0;}

python3解题:

# Definition for singly-linked list.class ListNode:    def __init__(self, val=0, next=None):        self.val = val        self.next = nextclass Solution:    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:        ret = ListNode(0)        tail = ret        carry = 0        while(l1 or l2):            val1 = l1.val if l1 else 0            val2 = l2.val if l2 else 0            sum = carry + val1 + val2            carry = sum // 10            tail.next = ListNode(sum % 10)            tail = tail.next            if(l1 is not None):                l1 = l1.next            if(l2 is not None):                l2 = l2.next        if (carry > 0):            tail.next = ListNode(1)        return ret.nextdef build_single_link(nums):    tail = head = ListNode(None)    for i in nums:        tail.next = ListNode(i)        tail = tail.next    return head.nextnums1 = [2, 4, 3]nums2 = [5, 6, 4]l1 = build_single_link(nums1)l2 = build_single_link(nums2)test = Solution()res = test.addTwoNumbers(l1, l2)while res:    print(res.val, end=' ')    res = res.nextprint()

标签: #python列表内的数字相加