力扣崩溃题:链表相加

复制代码
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;
}
相关推荐
CoderCodingNo19 分钟前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
大闲在人29 分钟前
7. 供应链与制造过程术语:“周期时间”
算法·供应链管理·智能制造·工业工程
小熳芋32 分钟前
443. 压缩字符串-python-双指针
算法
Charlie_lll42 分钟前
力扣解题-移动零
后端·算法·leetcode
chaser&upper42 分钟前
矩阵革命:在 AtomGit 解码 CANN ops-nn 如何构建 AIGC 的“线性基石”
程序人生·算法
weixin_499771551 小时前
C++中的组合模式
开发语言·c++·算法
iAkuya1 小时前
(leetcode)力扣100 62N皇后问题 (普通回溯(使用set存储),位运算回溯)
算法·leetcode·职场和发展
近津薪荼1 小时前
dfs专题5——(二叉搜索树中第 K 小的元素)
c++·学习·算法·深度优先
xiaoye-duck1 小时前
吃透 C++ STL list:从基础使用到特性对比,解锁链表容器高效用法
c++·算法·stl
松☆1 小时前
CANN与大模型推理:在边缘端高效运行7B参数语言模型的实践指南
人工智能·算法·语言模型