LeeCode: 2. Add Two Numbers(c语言)

  1. 这是第一次按照最初的思路写出来的
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
     struct ListNode* l = (struct ListNode*)malloc(sizeof(struct ListNode));
     l->next = NULL;
     struct ListNode* p = l;
        int flag=0;
        while(l1&&l2){
            int temp = l1->val+l2->val+flag;
            struct  ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
            node->next = NULL;
            p->next = node;
            p = node;
            node->val = temp%10;
            flag = temp/10;
            l1 = l1->next;
            l2 = l2->next;
        }
        if(l1){
            while(l1){
                int temp = l1->val+flag;
                struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
                node->next = NULL;
                p->next = node;
                p = node;
                node->val = temp%10;
                flag = temp/10;
                l1 = l1->next;
            }
            
        }else if(l2){
             while(l2){
                int temp = l2->val+flag;
                struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
                node->next = NULL;
                p->next = node;
                p = node;
                node->val = temp%10;
                flag = temp/10;
                l2 = l2->next;
            }
        }
        if(flag!=0){
             struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
                node->next = NULL;
                p->next = node;
                p = node;
                node->val =flag;
        }
        return l->next;
}

2.这是考虑到l1 l2不需要再保留了,减少分配空间的代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
     struct ListNode* l = (struct ListNode*)malloc(sizeof(struct ListNode));
     l->next = NULL;
     struct ListNode* p = l;
     int flag=0;
     while(l1||l2){
          int temp = (l1!=NULL?l1->val:0)+(l2!=NULL?l2->val:0)+flag;
          flag = temp/10;
          if(l1){
              l1->val = temp%10;
              p->next = l1;
              p = l1;
              l1 = l1->next;
              if(l2) l2 = l2->next;
          }else{
              l2->val = temp%10;
              p->next = l2;
              p = l2;
              l2 = l2->next;
          }          
        }
        if(flag!=0){
             struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
                node->next = NULL;
                p->next = node;
                p = node;
                node->val =flag;
        }
        return l->next;
}
image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Python语言特性 1 Python的函数参数传递 看两个如下例子,分析运行结果: 代码一: a = 1 def...
    伊森H阅读 8,233评论 0 15
  • Python语言特性 1 Python的函数参数传递 看两个如下例子,分析运行结果: 代码一: a = 1 def...
    时光清浅03阅读 3,437评论 0 0
  • You are given two non-empty linked lists representing two...
    KaelQ阅读 4,035评论 1 6
  • 每个人来到这个世界上, 都带着自己的使命。 而为了达成自己的使命, 每个人也都带着自己与生俱来的天生技能, 在修行...
    飞言米语阅读 5,017评论 0 1

友情链接更多精彩内容