# crack the code interview 2.4

255人阅读 评论(0)

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.

{
int value;
}

{

int pre = 0;
while (h1 != NULL && h2 != NULL)
{
int s = h1->value + h2->value + pre;
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;
}
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
{
pre = 0;
h = n;
}
h = h->next;
}
return start;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：26351次
• 积分：767
• 等级：
• 排名：千里之外
• 原创：54篇
• 转载：5篇
• 译文：0篇
• 评论：0条
评论排行