单链表实现大数相加

Posted zllwxm123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单链表实现大数相加相关的知识,希望对你有一定的参考价值。

字节跳动面试题:

两个数为正数。

用链表求和:

现场写的时候出了几个bug实在尴尬。。。

样例一

2 3 4 5
4 5 6
2 8 0 1

样例二

9 9 9
9 9 9
1 9 9 9

 

 

  1 #include <iostream>
  2 using namespace std;
  3 
  4 struct List
  5     int val;
  6     List * next;
  7 ;
  8 
  9 List* insert(List *node, int val)
 10     List *root = node;
 11     if(node == NULL)
 12         // cout << "!!" << endl;
 13         node = new List();
 14         root = node;
 15         node->val = val;
 16         node->next = NULL;
 17     else
 18         while(node->next != NULL)
 19             node = node->next;
 20         
 21         List * an = new List();
 22         an->val = val;
 23         an->next = node->next;
 24         node->next = an;
 25     
 26     return root;
 27 
 28 
 29 void print(List *an)
 30     while(an != NULL)
 31         cout << an->val<<" ";
 32         an = an->next;
 33     
 34     cout << endl;
 35 
 36 
 37 
 38 List * become(List *node)
 39     if(node == NULL) return NULL;
 40     List *pnext = NULL;
 41     List *parent = NULL;
 42     while(node != NULL)
 43         pnext = node->next;
 44         node->next = parent;
 45         parent = node;
 46         node = pnext;
 47     
 48     return parent;
 49 
 50 
 51 List * getsum(List *a, List *b)
 52     List *an = become(a);
 53     List *bn = become(b);
 54     List *cn = new List();
 55     int flag = 0;
 56     while(an != NULL && bn!=NULL)
 57         List *cnt = new List();
 58         cnt->val= (an->val + bn->val + flag)%10;
 59         if((an->val + bn->val + flag)>=10)
 60             flag = 1;
 61         else
 62             flag = 0;
 63         cnt->next = cn->next;
 64         cn->next = cnt;
 65         an = an->next;
 66         bn = bn->next;
 67     
 68     while(an != NULL)
 69         List *cnt = new List();
 70         if((an->val + flag)>=10)
 71             flag = 1;
 72         else
 73             flag = 0;
 74         cnt->val= (an->val + flag)%10;
 75         cnt->next = cn->next;
 76         cn->next = cnt;
 77         an = an->next;
 78     
 79     while(bn != NULL)
 80         List *cnt = new List();
 81         if((bn->val + flag)>=10)
 82             flag = 1;
 83         else
 84             flag = 0;
 85         cnt->val= (bn->val + flag)%10;
 86         cnt->next = cn->next;
 87         cn->next = cnt;
 88         bn = bn->next;
 89     
 90     if(flag == 1)
 91         List *cnt = new List();
 92         cnt->val= 1;
 93         cnt->next = cn->next;
 94         cn->next = cnt;
 95     
 96     return cn->next;
 97 
 98 
 99 
100 int main() 
101     string s;
102     getline(cin, s);
103     List *a = NULL, *b = NULL;
104     int cnt = 0;
105     s +=  ;
106     for(int i = 0; i < s.length(); i++)
107         if(isdigit(s[i]))
108             cnt = cnt*10 + s[i]-0;
109         else
110             a = insert(a, cnt);
111             cnt = 0;
112         
113     
114     getline(cin, s);
115     cnt = 0;
116     s +=  ;
117     for(int i = 0; i < s.length(); i++)
118         if(isdigit(s[i]))
119             cnt = cnt*10 + s[i]-0;
120         else
121             b= insert(b, cnt);
122             cnt = 0;
123         
124     
125     List * node = getsum(a,b);
126     print(node);
127     return 0;
128 

 

以上是关于单链表实现大数相加的主要内容,如果未能解决你的问题,请参考以下文章

大数相加常考面试题

栈实现两个大数相加

大数相乘相加相减相除

C语言使用栈实现String类型的两个大数相加

大数相加算法实现

大数相加算法实现