leetcode记录2.Add Two Numbers

本文详细解析了LeetCode上的经典题目“两数相加”(Add Two Numbers),通过链表的形式存储两个非负整数,逆序存放每位数字,实现两数相加并返回结果链表。文章提供了Python3实现代码,展示了如何处理进位和不同长度链表的相加操作。

leetcode记录2.Add Two Numbers

#
# @lc app=leetcode id=2 lang=python3
#
# [2] Add Two Numbers
#
# https://leetcode.com/problems/add-two-numbers/description/
#
# algorithms
# Medium (30.58%)
# Total Accepted:    777.6K
# Total Submissions: 2.5M
# Testcase Example:  '[2,4,3]\n[5,6,4]'
#
# 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.
# 
# 
#
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        result = ListNode(0)
        temp = result
        a = 0
        while l1 != None and l2 != None:
            b = (l1.val + l2.val + a) % 10
            result.next = ListNode(b)
            a = (l1.val + l2.val + a) // 10
            l1 = l1.next
            l2 = l2.next
            result = result.next
        
        if l1 == None:
            l1 = l2
        while l1 != None:
            b = (l1.val + a) % 10
            result.next = ListNode(b)
            a = (l1.val + a) // 10
            result = result.next
            l1 = l1.next
            
        if a != 0:
            result.next = ListNode(a)
        return temp.next


LeetCode 题目2《两数相加》是一道经典的算法题目,主要涉及基本的数组操作和整数计算,目标是给定两个非负整数,通过栈的数据结构来求它们的和。以下是使用 C 语言的一个简单解决方案: ```c #include <stdio.h> // 定义一个结构体存放链表节点 typedef struct ListNode { int val; struct ListNode *next; } ListNode; // 将输入的十进制数字转换成链表表示形式 ListNode* createListNode(int num) { ListNode* head = (num == 0) ? NULL : &head; while (num > 0) { head->val = num % 10; num /= 10; head = head->next; if (head == NULL) { head = (ListNode*)malloc(sizeof(ListNode)); head->next = NULL; } } return head; } // 将链表表示的两个数相加并返回结果链表 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode dummy(0), *curr = &dummy; int carry = 0; while (l1 != NULL || l2 != NULL) { int a = (l1 != NULL) ? l1->val : 0; int b = (l2 != NULL) ? l2->val : 0; int sum = a + b + carry; carry = sum / 10; // 计算进位 curr->next = createListNode(sum % 10); // 创建新的结点存储当前位的值 curr = curr->next; if (l1 != NULL) { l1 = l1->next; } if (l2 != NULL) { l2 = l2->next; } } // 如果最后还有进位,则在链表末尾添加一个结点表示 if (carry > 0) { curr->next = createListNode(carry); } return dummy.next; } int main() { ListNode* l1 = createListNode(2); l1->next = createListNode(4); l1->next->next = createListNode(3); ListNode* l2 = createListNode(5); l2->next = createListNode(6); l2->next->next = createListNode(4); ListNode* result = addTwoNumbers(l1, l2); while (result != NULL) { printf("%d", result->val); result = result->next; } return 0; } ``` 这个程序首先将两个输入的整数转换成链表的形式,然后逐位相加,每一步都处理了进位的情况,并保持链表结构。最终得到的结果也是一个链表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值