Add Two Numbers

本文深入探讨了链表加法的实现方法,提供了多种C++代码解决方案,包括使用递归和迭代的方式,以及如何处理进位等关键问题。通过详细的代码示例,读者可以了解到链表操作的具体细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyAnwser:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        auto* res = new ListNode(0);
        res->val = (l1->val + l2->val)%10;
        bool remainder = ((l1->val + l2->val)>=10) ? true:false;
        auto* cur = res;
        while(l1->next != 0 && l2->next != 0)
        {
            cur -> next = new ListNode(0);
            cur = cur -> next;
            l1 = l1->next;
            l2 = l2->next;
            cur->val = (l1->val + l2->val + (remainder ? 1:0) ) % 10;
            if(l1->val + l2->val  + (remainder ? 1:0) >= 10)
                remainder = true;
            else
                remainder = false;
        }
        while(l1->next != 0){
            l1 = l1->next;
            cur->next = new ListNode(0);
            cur = cur->next;
            cur->val = (l1->val + (remainder ? 1:0)) % 10;
            if((l1->val + (remainder ? 1:0)) >= 10)
                remainder = true;
            else
                remainder = false;
        }
        while(l2->next != 0){
            l2 = l2->next;
            cur->next = new ListNode(0);
            cur = cur->next;
            cur->val = (l2->val + (remainder ? 1:0)) % 10;
            if((l2->val + (remainder ? 1:0)) >= 10)
                remainder = true;
            else
                remainder = false;
        }
        if(remainder){
            cur->next = new ListNode(1);
        }
        return res;
    }
};

 

原作者:jhanggj

 

#define BASE_NUM 10

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
  
      if (l1 == NULL || l2 == NULL) {
        return NULL;
      }
  
      int sumOfAandB = 0;
      int remainder = 0;   
      bool createNewNodeFlag = false;
      
      //Create curNode  
      struct ListNode* curNode = (struct ListNode*) malloc(sizeof(struct ListNode));
      curNode->val = 0;
      curNode->next = NULL;
      //For return
      struct ListNode* returnListNode = curNode;
  
      while (l1 != NULL || l2 != NULL || createNewNodeFlag) {
        
        //STEP.1 Cal
        createNewNodeFlag = false;
        sumOfAandB = ((l1) ? l1->val : 0) + ((l2) ? l2->val : 0) + remainder;
        printf("sumOfAandB is %d\n", sumOfAandB);
        
        //STEP.2 decide to create NewNode
        //forward next one
        if(l1 != NULL)
          l1 = l1->next;
        if(l2 != NULL)
          l2 = l2->next;
        if (l1 != NULL || l2 != NULL) {
          createNewNodeFlag = true;
        }
        
        if (sumOfAandB > BASE_NUM) {
          createNewNodeFlag = true;
          curNode->val = sumOfAandB % BASE_NUM;
          remainder = 1;
        } else if (sumOfAandB == BASE_NUM) {
          createNewNodeFlag = true;
          curNode->val = 0;
          remainder = 1;
        } else if (sumOfAandB < BASE_NUM) {
          curNode->val = sumOfAandB % BASE_NUM;
          remainder = 0;
        }
        
        //STEP.3 Create the NewNode
        if (createNewNodeFlag) {
           curNode->next = (struct ListNode*) malloc(sizeof(struct ListNode));
           curNode = curNode->next;
           //Reset 
           curNode->val = 0;
           curNode->next = NULL; //be noticed
        } 
      }
  
     return returnListNode;
}

原作者:Leeeeen

 

class Solution {
public:
    //ListNode*
    static ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        auto lhs = l1, rhs = l2;
        int mul = 1;
        bool flow = false;
        auto *result = new ListNode(0);
        auto node_it = result;
        while(lhs || rhs) {
            if(lhs)
                node_it->val += lhs->val;
            if(rhs)
                node_it->val += rhs->val;
            if(lhs || rhs || (node_it->val > 9)) {
                auto *it = new ListNode(0);
                if(node_it->val > 9) {
                    it->val += 1;
                    node_it->val -= 10;
                }
                lhs = (lhs && lhs->next) ? lhs->next : nullptr;
                rhs = (rhs && rhs->next) ? rhs->next : nullptr;
                if(lhs || rhs || it->val > 0) {
                    node_it->next = it;
                    node_it = node_it->next;
                } 
            }

        }
        return result;
    }
};

原作者:Favoryoung

 

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *ll1, *ll2;
    struct ListNode *head = NULL;
    struct ListNode **lp = &head;
    int carry = 0, sum = 0, val1, val2;
    for (ll1 = l1, ll2 = l2; ll1 || ll2;) {
        val1 = ll1 ? ll1->val : 0;
        val2 = ll2 ? ll2->val : 0;
        sum = val1 + val2 + carry;
        *lp = malloc(sizeof(struct ListNode));
        (*lp)->val = sum % 10;
        (*lp)->next = NULL;
        carry = sum / 10;
        lp = &(*lp)->next;
        ll1 = ll1 ? ll1->next : NULL;
        ll2 = ll2 ? ll2->next : NULL;
    }
    if (carry) {
        *lp = malloc(sizeof(struct ListNode));
        (*lp)->val = carry;
        (*lp)->next = NULL;
    }
    return head;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值