LeetCode 2. Add Two Numbers

Problem Description:

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

分析题目:该题实际上就是求两个链表相加,比较简单,只需要注意一下链表位数不等与进位的问题即可。

源程序(包含输入与输出):

/*
** 
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode* List;
struct ListNode {
     int val;
     struct ListNode *next;
};

void Print(List L);
List ReadInput(void);
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2);
int main()
{
	List l1,l2,l;
	l1 = ReadInput();
	l2 = ReadInput();
//	Print(l1);
//	Print(l2);
	l = addTwoNumbers( l1, l2);
	Print(l);

	return 0;
}

List ReadInput(void)
{
	List L,P,t;
	L = (List)malloc(sizeof(struct ListNode));
	P = L;
	scanf("%d",&P->val);
	for( ; getchar() != '\n'; )
	{
		t = P;
		P = (List)malloc(sizeof(struct ListNode));
		t->next = P;
		scanf("%d",&P->val);
	} 
	P->next = NULL;
	return L;
}
void Print(List L)
{
	List P=L;
	for(; P!=NULL; P = P->next)
		printf("%d ",P->val);
	printf("\n"); 
} 
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *p1, *p2 , *l, *t, *p;
    int c=0;	/* 进位 */ 
    l = (struct ListNode*)malloc(sizeof(struct ListNode)); 
    p = l;
    for(p1 = l1, p2 = l2; p1 != NULL && p2 != NULL; p1 = p1->next, p2 = p2->next)
    {
        p->val = p1->val + p2->val + c;
//        printf("%d p %d p1 %d p2\n",p->val,p1->val,p2->val);
        t = p;
        p = (struct ListNode*)malloc(sizeof(struct ListNode)); 
        t->next = p;
        c = 0;
        
        if(t->val > 9)
        {
        	t->val -= 10;
        	c = 1;
		}
    }
    
    /*
    **	p1=NULL, p1的位数小于p2
	**	p+p2;
	*/ 
    if( !p1 )
    {
    	for( ; p2 != NULL; p2 = p2->next) 
    	{
    		p->val = p2->val + c;
       		t = p;
        	p = (struct ListNode*)malloc(sizeof(struct ListNode)); 
        	t->next = p;
        	if(t->val > 9)	{
        		c=1;		/* 若进位则加1 */
				t->val -= 10; 
			}
        	else	c = 0;				
		}	
	}
	/*
    **	p2=NULL, p2的位数小于p1
	**	p+p1;
	*/ 
	if( !p2 )
    {
    	for( ; p1 != NULL; p1 = p1->next) 
    	{
    		p->val = p1->val + c;
       		t = p;
        	p = (struct ListNode*)malloc(sizeof(struct ListNode)); 
        	t->next = p;
        	if(t->val > 9)	{
        		c=1;		/* 若进位则加1 */
				t->val -= 10; 
			}
        	else	c = 0;		
		}	
	}
	/*
	**	p1,p2位数一样,但是最高位进位 
	*/
	if(c)
	{
		p->val = c;
       	t = p;
        p = (struct ListNode*)malloc(sizeof(struct ListNode)); 
        t->next = p;
	} 
	free(p);
	t->next	= NULL;
	return l; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值