LeetCode第二题--两数相加

这篇博客介绍了如何使用C++和Python实现两个链表相加,通过遍历链表并处理进位来创建新的链表。在C++中,创建了一个额外的节点指针移动并更新值;Python中提供了两种实现方式,思路相同但代码更为简洁。
摘要由CSDN通过智能技术生成

C++

链表通过指针相连起来,只要有头指针指向第一个节点就可以找到整个的链表,然后链表的创建和查找需要定义一个额外的节点指针,用来不断的指向next指针指向的内存空间

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head = new ListNode(-1);
        ListNode* h = head; //移动指针
        int sum = 0;        //每一位的加和
        int carry = 0;      //进位标志
        while(l1!=nullptr||l2!=nullptr||carry){
            sum = carry;    //每次循环就是运算不同位的加和,每次开始前都要重新赋carry非0即1
                            //这样也包含了最后一位进一的情况
            if(l1!=nullptr){
                sum+=l1->val;
                l1 = l1->next;//移动l1指针
            }
            if(l2!=nullptr){
                sum+=l2->val;
                l2 = l2->next;
            }
            carry=sum>=10?1:0;
            h->next = new ListNode(sum%10);     
            h = h->next;
        }
        return head->next;                      //返回第二个节点后的链表

    }
};

Python

法一(思路同上):

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        head = h = ListNode(-1)
        sum = 0
        carry  = False
        while l1 or l2:
            sum = 0
            if l1:
                sum += l1.val
                l1 = l1.next
            if l2:
                sum += l2.val
                l2 = l2.next
            if carry:
                sum+=1
            carry = 1 if sum>=10 else 0
            h.next = ListNode(sum%10)
            h = h.next
        if carry: h.next = ListNode(1)
        return head.next

法二(思路一样,就是更简洁):
divmod(a, b)函数返回a/b的取整和a%b

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        current = dummy = ListNode()
        carry, value = 0, 0
        while carry or l1 or l2:
            value = carry
            if l1: l1, value = l1.next, l1.val + value
            if l2: l2, value = l2.next, l2.val + value
            carry, value = divmod(value, 10)
            current.next = ListNode(value)
            current = current.next
        return dummy.next
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值