LeetCode 445. 两数相加 II(C、C++、python)

给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

进阶:

如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。

示例:

输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7

C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

int getlen(struct ListNode* head)
{
    int count=0;
    while(head)
    {
        count++;
        head=head->next;
    }
    return count;
}

struct ListNode* reversed(struct ListNode* head)
{
	if(NULL==head || NULL==head->next)
	{
	    return head;
	}
	struct ListNode* pcur=head;
	struct ListNode* pnew=NULL;
	struct ListNode* pnext=NULL;
	while(pcur)
	{
	    pnext=pcur->next;
	    pcur->next=pnew;
	    pnew=pcur;
	    pcur=pnext;
	}
	return pnew;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) 
{
    l1=reversed(l1);
    l2=reversed(l2);
    int count1=getlen(l1);
    int count2=getlen(l2);
    struct ListNode* large;
    struct ListNode* small;
    if(count1>count2)
    {
        large=l1;
        small=l2;
    }
    else
    {
        large=l2;
        small=l1;
    }
    struct ListNode* p=(struct ListNode*)malloc(sizeof(struct ListNode));
    p->next=NULL;
    int add=0;
    int n=small->val+large->val+add;
    if(n<10)
    {
        p->val=n;
        add=0;
    }
    else
    {  
        p->val=n%10;      
        add=n/10;
    }
    small=small->next;
    large=large->next;
    struct ListNode* head=p;
    while(small)
    {
        p->next=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->next->next=NULL;
        int num=small->val+large->val+add;
        if(num<10)
        {
            p->next->val=num;
            p=p->next;
            add=0;
        }
        else
        {
            p->next->val=num%10;
            p=p->next;
            add=num/10;
        }
        small=small->next;
        large=large->next;
    }
    while(large)
    {
        int sum=add+large->val;
        p->next=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->next->next=NULL;
        if(sum<10)
        {
            p->next->val=sum;
            add=0;
            p=p->next;
        }
        else
        {
            p->next->val=sum%10;
            add=sum/10;
            p=p->next;
        }
        large=large->next;
    }
    if(add)
    {
        p->next=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->next->next=NULL;
        p->next->val=add;
    }
    return reversed(head);    
}

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverse(ListNode* head)
    {
        if(NULL==head || NULL==head->next)
        {
            return head;
        }
        ListNode* pnext=NULL;
        ListNode* pnew=NULL;
        ListNode* pcur=head;
        while(pcur)
        {
            pnext=pcur->next;
            pcur->next=pnew;
            pnew=pcur;
            pcur=pnext;
        }
        return pnew;
    }
    int getlen(ListNode* head)
    {
        int count=0;
        while(head)
        {
            count++;
            head=head->next;
        }
        return count;
    }
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
    {
        l1=reverse(l1);
        l2=reverse(l2);
        int count1=getlen(l1);
        int count2=getlen(l2);
        ListNode* large;
        ListNode* small;
        if(count1>count2)
        {
            large=l1;
            small=l2;
        }
        else
        {
            large=l2;
            small=l1;
        }
        int add=0;       
        int n=large->val+small->val+add;
        ListNode* p;
        if(n<10)
        {
            p=new ListNode(n);
        }
        else
        {
            p=new ListNode(n%10);
            add=n/10;
        }
        small=small->next;
        large=large->next;
        ListNode* head=p;
        while(small)
        {
            int num=small->val+large->val+add;
            if(num<10)
            {
                p->next=new ListNode(num);
                add=0;
                p=p->next;
            }
            else
            {
                p->next=new ListNode(num%10);
                add=num/10;
                p=p->next;
            }
            small=small->next;
            large=large->next;
        }
        while(large)
        {
            int sum=add+large->val;
            if(sum<10)
            {
                p->next=new ListNode(sum);
                add=0;
                p=p->next;
            }
            else
            {
                p->next=new ListNode(sum%10);
                add=sum/10;
                p=p->next;
            }
            large=large->next;
        }
        if(add)
        {
            p->next=new ListNode(add);
        }
        return reverse(head);              
    }
};

python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getlen(self, head):
        count=0
        while head:
            count+=1
            head=head.next
        return count
    def rev(self, head):
        if None==head or None==head.next:
            return head
        pcur=head
        pnew=None
        pnext=None
        while pcur:
            pnext=pcur.next
            pcur.next=pnew
            pnew=pcur
            pcur=pnext
        return pnew
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        l1=self.rev(l1)
        l2=self.rev(l2)
        count1=self.getlen(l1)
        count2=self.getlen(l2)
        large=None
        small=None
        if count1>count2:
            large=l1
            small=l2
        else:
            large=l2
            small=l1
        p=None
        ad=0
        num=large.val+small.val+ad
        if num<10:
            p=ListNode(num)
            ad=0
        else:
            p=ListNode(num%10)
            ad=num//10
        large=large.next
        small=small.next
        head=p
        #p=p.next
        while small:
            num=small.val+large.val+ad
            if num<10:
                p.next=ListNode(num)
                ad=0
            else:
                p.next=ListNode(num%10)
                ad=num//10
            p=p.next
            small=small.next
            large=large.next
        while large:
            num=large.val+ad
            if num<10:
                p.next=ListNode(num)
                ad=0
            else:
                p.next=ListNode(num%10)
                ad=num//10
            p=p.next
            large=large.next
        if ad:
            p.next=ListNode(ad)
        return self.rev(head)

猜你喜欢

转载自blog.csdn.net/qq_27060423/article/details/84996602