# crack the code interview 2.4

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

//You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

struct LinkNode
{
LinkNode * next;
int value;
}

LinkNode * BigAdd(LinkNode * n1, LinkNode * n2)
{
LinkNode * sum = NULL;
LinkNode * start = sum;

LinkNode * h1 = n1;
LinkNode * h2 = n2;
int pre = 0;
while (h1 != NULL && h2 != NULL)
{
int s = h1->value + h2->value + pre;
LinkNode * n = new LinkNode(s, NULL);
if (s >= 10)
{
n->value = (s) % 10;
pre = s - (s/10) * 10;
}
if (sum == NULL)
{
sum = n;
}
else
sum->next = n;
h1 = h1->next;
h2 = h2->next;
}
LinkNode * h = NULL;
if (h1 != NULL)
h = h1;
else (h2 != NULL)
h = h2;

sum->next = h;
while (pre != 0)
{
int s = pre + h->value;
if (h != NULL)
{
if (s >= 10)
{
h->value = s % 10;
pre = s - (s/10) * 10;
}
}
else
{
LinkNode * n = new LinkNode(pre);
pre = 0;
h = n;
}
h = h->next;
}
return start;
}


#### cracking the coding interview 中文版 （程序员面试金典）

2014-10-19 11:32:00

#### 笔记摘录——Cracking the code interview

2014-11-12 23:42:47

#### Cracking the code interview总结

2016-04-14 14:09:30

#### Cracking the Coding Interview 6th 第6版 高清完整版PDF

2016年06月18日 53.45MB 下载

#### cracking the code interview——c++实现

2016-07-09 09:03:53

#### 经典算法题目：Cracking the coding interview 问题与解答

2016-10-31 00:39:47

#### Cracking the coding interview---中文解析版

2014-09-24 14:58:16

#### [程序员面试金典] 英文原版非扫描版 Cracking the Coding Interview(6th) 189道问题和答案

2018年02月08日 81.29MB 下载

#### Cracking the Coding Interview 150题（一）

2015-03-24 23:16:45

#### Cracking the coding interview（原版）

2014-07-01 11:51:19