Add Two Numbers --leetcode

来源:互联网 发布:淘宝卖家和买家都被骗 编辑:程序博客网 时间:2024/06/14 13:52

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

函数接口:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {}

我的函数:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {char f=0;unsigned int i=0,j=0;struct ListNode *L,*L_long,*L_short;L=l1;//统计l1数据长度iwhile(L!=NULL){i++;L=L->next;}L=l2;//统计l2数据长度jwhile(L!=NULL){j++;L=L->next;}if(i>j){L_short=l2;L_long=l1;}else{L_short=l1;L_long=l2;}L=L_long;do{if(f==1){L_long->val+=L_short->val;L_long->val+=1;//printf("%d ",L_long->val);f=0;}else{L_long->val+=L_short->val;//printf("%d ",L_long->val);}if(L_long->val>=10){L_long->val%=10;f=1;}L_short=L_short->next;if(L_short!=NULL){    L_long=L_long->next;}}while(L_short!=NULL);aa:if(f==1&&L_long->next==NULL){ //判断L_long是否为链表的最后一个节点,是最后一个节点,进位--添加节点。    f=0;struct ListNode *s;s=(struct ListNode *)malloc(sizeof(struct ListNode));L_long->next=s;s->val=1;s->next=NULL;}if(f==1&&L_long->next!=NULL){ //判断L_long是否为链表的最后一个节点,不是最后一个节点。    f=0;    L_long=L_long->next;L_long->val+=1;if(L_long->val==10){      //判断是否进位,进位。    f=1;    L_long->val=0;    goto aa;}}    return L;}

自己写的测试函数(模拟这个题目的测试环境):

#include<stdlib.h>#include<stdio.h>struct ListNode {  int val;  struct ListNode *next;};/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {char f=0;unsigned int i=0,j=0;struct ListNode *L,*L_long,*L_short;L=l1;//统计l1数据长度iwhile(L!=NULL){i++;L=L->next;}L=l2;//统计l2数据长度jwhile(L!=NULL){j++;L=L->next;}if(i>j){L_short=l2;L_long=l1;}else{L_short=l1;L_long=l2;}L=L_long;do{if(f==1){L_long->val+=L_short->val;L_long->val+=1;//printf("%d ",L_long->val);f=0;}else{L_long->val+=L_short->val;//printf("%d ",L_long->val);}if(L_long->val>=10){L_long->val%=10;f=1;}L_short=L_short->next;if(L_short!=NULL){    L_long=L_long->next;}}while(L_short!=NULL);aa:if(f==1&&L_long->next==NULL){ //判断L_long是否为链表的最后一个节点,是最后一个节点,进位--添加节点。    f=0;struct ListNode *s;s=(struct ListNode *)malloc(sizeof(struct ListNode));L_long->next=s;s->val=1;s->next=NULL;}if(f==1&&L_long->next!=NULL){ //判断L_long是否为链表的最后一个节点,不是最后一个节点。    f=0;    L_long=L_long->next;L_long->val+=1;if(L_long->val==10){      //判断是否进位,进位。    f=1;    L_long->val=0;    goto aa;}}    return L;}void main(){struct ListNode *l1,*l2;struct ListNode *L;struct ListNode *p;int i,j;l1=(struct ListNode *)malloc(sizeof(struct ListNode));l1->next=NULL;printf("Please input the length of the l1:");scanf("%d",&i);for(;i>0;i--){p=(struct ListNode *)malloc(sizeof(struct ListNode));scanf("%d",&p->val);p->next=l1->next;l1->next=p;}l1=l1->next;//表头非空l2=(struct ListNode *)malloc(sizeof(struct ListNode));l2->next=NULL;printf("Please input the length of the l2:");scanf("%d",&j);for(;j>0;j--){p=(struct ListNode *)malloc(sizeof(struct ListNode));scanf("%d",&p->val);p->next=l2->next;l2->next=p;}l2=l2->next;//表头非空L=addTwoNumbers(l1,l2);printf("\nThe resault is:");while(L->next!=NULL){printf("%d->",L->val);L=L->next;}printf("%d\n",L->val);return;}







0 0
原创粉丝点击