|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 本空逐弥 于 2021-7-25 22:35 编辑
题目
给出两个非空的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位数字。如果我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字0之外,这两个数都不会以0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
}
狗代码如下
- #include <stdio.h>
- #include <malloc.h>
- #include <math.h>
- #define LEN sizeof(struct ListNode)
- struct ListNode
- {
- int val;
- struct ListNode *next;
- };
- int n;
- struct ListNode*creat()
- {
- struct ListNode*head;
- struct ListNode*p1,*p2;
- n=0;
- p1=p2=(struct ListNode*)malloc(LEN);
- scanf("%d",&p1->val);
- head=NULL;
- while(p1->val!=-1)
- {
- n=n+1;
- if(n==1)head=p1;
- else p2->next=p1;
- p2=p1;
- p1=(struct ListNode*)malloc(LEN);
- scanf("%d",&p1->val);
- }
- p2->next=NULL;
- return(head);
- }
- struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
- {
- int num1=0,num2=0,num=0;
- int count=0,s=0,t;
- struct ListNode*p,*q;
- p=NULL;
- q=p;
- while(l1->val!=-1)
- {
- num1=num1+(l1->val)*(10^count);
- l1=l1->next;
- count++;
- }
- count=0;
- while(l2->val!=-1)
- {
- num2=num2+(l2->val)*(10^count);
- l2=l2->next;
- count++;
- }
- count=0;
- num=num1+num2;
- t=num;
- if(num==0)
- {
- p->val=0;
- p->next=NULL;
- }
- else
- {
- while(t)
- {
- t=t/10;
- s++;
- }
- p->val=num/(10^count)%10;
- count++;
- for(int i=2;i<=t;i++)
- {
- p=p->next;
- p->val=num/(10^count)%10;
- count++;
- }
- p->next=NULL;
- }
- return q;
- }
- void print(struct ListNode*head)
- {
- struct ListNode*p;
- p=head;
- if(head!=NULL)
- do
- {
- printf("%d ",p->val);
- p=p->next;
- }while(p!=NULL);
- }
- int main()
- {
- struct ListNode *l1,*l2,*head;
- l1=creat();
- l2=creat();
- head=addTwoNumbers(l1,l2);
- print(head);
- return 0;
- }
复制代码
求教大佬~
有好多问题
- #include <stdio.h>
- #include <malloc.h>
- #include <math.h>
- #define LEN sizeof(struct ListNode)
- struct ListNode
- {
- int val;
- struct ListNode *next;
- };
- int n;
- struct ListNode*creat()
- {
- struct ListNode*head;
- struct ListNode*p1,*p2;
- n=0;
- p1=p2=(struct ListNode*)malloc(LEN);
- scanf("%d",&p1->val);
- head=NULL;
- while(p1->val!=-1)
- {
- n=n+1;
- if(n==1)head=p1;
- else p2->next=p1;
- p2=p1;
- p1=(struct ListNode*)malloc(LEN);
- scanf("%d",&p1->val);
- }
- p2->next=NULL;
- return(head);
- }
- struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
- {
- int num1=0,num2=0,num=0;
- //int count=0,s=0,t;
- int count=0;
- struct ListNode*p,*q;
- p=NULL;
- q=p;
- //while(l1->val!=-1)
- while(l1)
- {
- //num1=num1+(l1->val)*(10^count);
- num1 = num1 + l1->val * pow(10, count);
- l1=l1->next;
- count++;
- }
- count=0;
- //while(l2->val!=-1)
- while(l2)
- {
- /*num2=num2+(l2->val)*(10^count);*/
- num2 = num2 + l2->val * pow(10, count);
- l2=l2->next;
- count++;
- }
- struct ListNode *head = NULL;
- p = q = NULL;
- num = num1 + num2;
- do {
- p = malloc(sizeof(struct ListNode));
- p->next = NULL;
- p->val = num % 10;
- num /= 10;
- if(!head) head = q = p;
- else q->next = p, q = q->next;
- } while(num);
- return head;
- /*
- count=0;
- num=num1+num2;
- t=num;
- if(num==0)
- {
- // 不申请内存?
- p->val=0;
- p->next=NULL;
- }
- else
- {
- while(t)
- {
- t=t/10;
- s++;
- }
- p->val=num/(10^count)%10;
- count++;
- for(int i=2;i<=t;i++)
- {
- p=p->next;
- p->val=num/(10^count)%10;
- count++;
- }
- p->next=NULL;
- }
- return q;
- */
- }
- void print(struct ListNode*head)
- {
- struct ListNode*p;
- p=head;
- if(head!=NULL)
- do
- {
- printf("%d ",p->val);
- p=p->next;
- }while(p!=NULL);
- }
- int main()
- {
- struct ListNode *l1,*l2,*head;
- l1=creat();
- l2=creat();
- head=addTwoNumbers(l1,l2);
- print(head);
- // 又不释放内存
- return 0;
- }
复制代码
|
|