力扣崩溃题:链表相加

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){

    int sum=0, carry = 0;
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* p = head;

    while(l1 || l2 || carry) {
        sum = 0;
        if(l1) {
            sum += l1->val;
            l1 = l1->next;
        }

        if(l2) {
            sum += l2->val;
            l2 = l2->next;
        }
        sum += carry;

        struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
        newNode->val = sum >=10 ? sum%10 : sum;
        newNode->next = NULL;            
        carry = sum >=10 ? 1 : 0;
        
        p->next = newNode;
        p = p->next;
    }
    return head->next;

}

由于力扣的用例太大了,导致笔者的方法用不了,就是下面的,力扣你无敌了

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* addTwoNumbers(struct ListNode* head1, struct ListNode* head2){
    if(head1->val+head2->val<10&&head1->next==NULL&&head2->next==NULL)
    {
        head1->val=head1->val+head2->val;
        return head1;
    }
     if(head1->val+head2->val==10&&head1->next==NULL&&head2->next==NULL)
    {
        head1->val=0;
        struct ListNode*point3=(struct ListNode*)malloc(sizeof(struct ListNode));
    point3->next=NULL;
    point3->val=1;
    head1->next=point3;
        return head1;
    }
long long count1=0;
long long count2=0;
long long arr1[1000];
long long arr2[1000];
int i=0;
int j=0;
struct ListNode*point1=head1;
struct ListNode*point2=head2;
while(point1)
{
    arr1[i]=point1->val;
    i++;
    point1=point1->next;
}
while(point2)
{
    arr2[j]=point2->val;
    j++;
    point2=point2->next;
}
long good=1;
long bad=1;
for(int x=0;x<i;x++)
{
    count1=count1+good*arr1[x];
    good=good*10;
}
for(int x=0;x<j;x++)
{
    count2=count2+bad*arr2[x];
    bad=bad*10;
}
long long total=count1+count2;
int t=10;
int a=1;
int z=0;
long arr5[1000];
long count3=total;
while(count3!=0)
{
  arr5[z]=count3%10;
  z++;
  count3=count3/10;
}
free(head1);
head1=(struct ListNode*)malloc(sizeof(struct ListNode)*z);
head1->val=arr5[0];
head1->next=NULL;
struct ListNode*point4=head1;
for(int x=1;x<z;x++)
{
    struct ListNode*point3=(struct ListNode*)malloc(sizeof(struct ListNode));
    point3->next=NULL;
    point3->val=arr5[x];
    point4->next=point3;
    point4=point4->next;
}
return head1;
}
相关推荐
查理零世25 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
仟濹25 分钟前
【二分搜索 C/C++】洛谷 P1873 EKO / 砍树
c语言·c++·算法
紫雾凌寒34 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
京东零售技术1 小时前
AI Agent实战:打造京东广告主的超级助手 | 京东零售技术实践
算法
且听风吟ayan2 小时前
leetcode day19 844+977
leetcode·c#
MiyamiKK572 小时前
leetcode_位运算 190.颠倒二进制位
python·算法·leetcode
C137的本贾尼2 小时前
解决 LeetCode 串联所有单词的子串问题
算法·leetcode·c#
青橘MATLAB学习2 小时前
时间序列预测实战:指数平滑法详解与MATLAB实现
人工智能·算法·机器学习·matlab
lingllllove2 小时前
matlab二维艾里光束,阵列艾里光束,可改变光束直径以及距离
开发语言·算法·matlab
88号技师2 小时前
2025年2月一区SCI-海市蜃楼搜索优化算法Mirage search optimization-附Matlab免费代码
开发语言·人工智能·算法·机器学习·matlab·优化算法