题解 | #跳台阶#
两个链表生成相加链表
http://www.nowcoder.com/practice/c56f6c70fb3f4849bc56e33ff2a50b6b
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
struct ListNode *reclist(struct ListNode *p)
{
if( p==NULL || p->next==NULL)
{
return p;
}
struct ListNode *newcode=reclist(p->next);
p->next->next=p;
p->next=NULL;
return newcode;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
struct ListNode *a=reclist(head1);
struct ListNode *b=reclist(head2);
struct ListNode *s=NULL;
struct ListNode *head=s;
int jin=0;
while(a!=NULL || b!=NULL || jin==1 )
{
struct ListNode *newnode=(struct ListNode *)malloc(sizeof(struct ListNode));
int value1=(a==NULL)?0:a->val;
int value2=(b==NULL)?0:b->val;
newnode->val=value1+value2+jin;
newnode->next=NULL;
jin=0;
if(newnode->val >=10){
newnode->val=newnode->val-10;
jin=1;
}
if(s==NULL)
{
s=newnode;
head=s;
}else
{
s->next=newnode;
s=s->next;
}
a=(a==NULL)?NULL:a->next;
b=(b==NULL)?NULL:b->next;
}
return reclist(head);
}