add-two-numbers

【题目描述】
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes
contain a single digit. Add the two numbers and return it as
a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
题目的大概含义是:给两条链表,要求对两条链表进行相加,但是必须要有进位。
【题目解答】
方法一:
分别遍历两条链表,如果一条链表长而另外一条链表短,因此在将短的遍历完成之后,退出当前循环,如果两条链表中还有一条为空则在此时,将链表结点的值加上进位,从而链接在新链表的后面,从而返回即可,注意此处需要构造一个带头结点的新链表
时间复杂度:O(N)
空间复杂度:O(N)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        if(l1==NULL&&l2==NULL)
            return NULL;
        int flag=0;//表示进位
        ListNode *tmp=new ListNode(-1);
        ListNode *newhead=tmp;
        while(l1&&l2)
        {
            int sum=flag+l1->val+l2->val;
            flag=sum/10;
            ListNode *node=new ListNode(sum%10);
            newhead->next=node;
            newhead=newhead->next;
            l1=l1->next;
            l2=l2->next;
            //当两条链表都为空之时,即走到链表的末尾,因此此时应该跳出循环
            if(l1==NULL&&l2==NULL)
                break;
            //如果之时单条链表为空,则在此链表的后面构造新结点,等待两条链表都为空
            if(l1==NULL)
                l1=new ListNode(0);

            if(l2==NULL)
                l2=new ListNode(0);
        }
        //当将两条链表的所有结点相加完成之后,有可能最后还有一个进位
        if(flag!=0)
        {
            ListNode *node=new ListNode(flag);
            newhead->next=node;
            newhead=newhead->next;
        }
        newhead->next=NULL;
        return tmp->next;
    }
};

方法二:
此种方式是,也是利用遍历每一条链表,当走到哪一条链表为空之时,在此处并不退出此循环,而是在这个空链表的后面重新构造一个结点,并将原本为空的结点指向这个新构造的结点,当然在构造这个结点的时候,不能影响与另外一条链表相加的情况,最后直到两条链表均为空时即可。
时间复杂度:O(N)
空间复杂度:O(N)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        if(l1==NULL&&l2==NULL)
            return NULL;
        int flag=0;//表示进位
        ListNode *tmp=new ListNode(-1);
        ListNode *newhead=tmp;
        int sum=0;
        while(l1&&l2)
        {
            sum=flag+l1->val+l2->val;
            ListNode *node=new ListNode(sum%10);
            flag=sum/10;
            newhead->next=node;
            newhead=newhead->next;
            l1=l1->next;
            l2=l2->next;
        }
        //当两条链表中有一条为空,则退出上面的循环,但是假定l1不为空,刚才的flag有进位,而l1
        //后面的数字也全为9,因此在这里还是需要进位
        while(l1!=NULL)
        {
            sum=flag+l1->val;
            flag=sum/10;

            ListNode *node=new ListNode(sum%10);
            newhead->next=node;
            newhead=newhead->next;
            l1=l1->next;
        }
        while(l2!=NULL)
        {
            sum=flag+l2->val;
            flag=sum/10;

            ListNode *node=new ListNode(sum%10);
            newhead->next=node;
            newhead=newhead->next;
            l2=l2->next;
        }
        //当将两条链表的所有结点相加完成之后,有可能最后还有一个进位
        if(flag!=0)
        {
            ListNode *node=new ListNode(flag);
            newhead->next=node;
            newhead=newhead->next;
        }
        newhead->next=NULL;
        return tmp->next;
    }
};

猜你喜欢

转载自blog.csdn.net/aaronlanni/article/details/81475031