题解 | #跳台阶#

两个链表生成相加链表

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);

}
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 11:29
已编辑
斯卡蒂味的鱼汤:知道你不会来数马,就不捞你😂最近数马疯狂扩招,招聘要求挺低的,你能力肯定够,应该就是因为太强了,知道你不会来才不捞你
投递腾讯云智研发等公司7个岗位
点赞 评论 收藏
分享
10-28 17:30
已编辑
华东交通大学 Java
想进开水团喝开水:字节的hr的本职工作就是黄金矿工
秋招笔试记录
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务