Leetcode 2 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

實際上是342+465=807 只不過是用鏈表反向存儲每一位數字。

下面直接貼代碼:

#include<stdio.h>
#include<math.h>
#include<malloc.h>
/*
*u010498696 ac 16ms
*/
struct ListNode{
	int val;
	struct ListNode *next;
};

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
	
	struct ListNode* head = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode* new_node;
	struct ListNode* p;
	int carry=0,sum,val1,val2;//進位
	
	p = head;
	while(l1!=NULL || l2!=NULL || carry!=0)
	{
		val1 = (l1==NULL) ? 0 : l1->val;
		val2 = (l2==NULL) ? 0 : l2->val;
		sum = val1 + val2 + carry;
		carry = sum/10;
		p->val = sum%10;
		p->next = NULL;

		l1 = (l1==NULL) ? NULL : l1->next;
		l2 = (l2==NULL) ? NULL : l2->next;
		//判斷是否有必要新建節點
		if(l1!=NULL || l2!=NULL || carry!=0)
		{
			new_node = (struct ListNode *)malloc(sizeof(struct ListNode));
			new_node->next = NULL;
			new_node->val = 1;//指定爲1,若進位必爲1
			p->next = new_node;
			p = p->next;
		}
	}
	return head;
}

void main()
{
	struct ListNode *l,*p;
	struct ListNode *l0 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l1 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l2 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l3 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l4 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l5 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l6 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l7 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l8 = (struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *l9 = (struct ListNode *)malloc(sizeof(struct ListNode));
	l0->val = 9;
	l1->val = 1;
	l2->val = 9;
	l3->val = 9;
	l4->val = 9;
	l5->val = 9;
	l6->val = 9;
	l7->val = 9;
	l8->val = 9;
	l9->val = 9;


	l1->next=l2;
	l2->next=l3;
	l3->next=l4;
	l4->next=l5;
	l5->next=l6;
	l6->next=l7;
	l7->next=l8;
	l8->next=l9;
	l9->next=NULL;
	l0->next=NULL;
		
	l = addTwoNumbers(l0,l1);
	for(p=l;p!=NULL;p=p->next)
		printf("%d",p->val);
}

附java 代碼,我用eclipse運行沒錯,但是提交後總是顯示輸出不對

package solution;
/**
 * 
 * @author u010498696
 *
 */
public class Solution2 {
	public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
		ListNode p=l1;
		int number1=0,number2=0,mul1=0,mul2=0,sum;

		for(p = l1; p != null; p=p.next){
			number1 = (int) (number1 + p.val*Math.pow(10,mul1));
			mul1++;
		}
		for(p = l2; p != null; p=p.next){
			number2 = (int) (number2 + p.val*Math.pow(10,mul2));
			mul2++;
		}
		
		int temp_len = (mul1>=mul2)?mul1:mul2;//兩個數的最大長度
		int len=temp_len;
		sum = number1 +number2;
		//根據sum求長度,要麼爲temp_len要麼爲temp_len+1
		if(sum>=Math.pow(10, temp_len-1) && sum < Math.pow(10, temp_len))
			len = temp_len;
		else if(sum>=Math.pow(10, temp_len))
			len = temp_len+1;
		
		//獲取每一位數,構造鏈表節點 從低位開始賦值
		ListNode l3 = new ListNode(0);
		p=l3;

		for(int i=1;i<=len;i++){
			p.val =sum%10;
			sum = sum/10;
			if(sum>0){
				ListNode nextNode = new ListNode(0);
				nextNode.next=null;
				p.next = nextNode;
				p=p.next;
			}
		}
		return l3;
	}
	public static void main(String[] args){
		ListNode p,l;
	/*	ListNode l1 = new ListNode(2);
		ListNode l2 = new ListNode(3);
		ListNode l3 = new ListNode(4);
		ListNode l4 = new ListNode(1);
		ListNode l5 = new ListNode(5);
		ListNode l6 = new ListNode(6);

		l1.next=l2;
		l2.next=l3;
		l3.next=null;
		//
		l4.next=l5;
		l5.next=l6;
		l6.next=null;
		*/
		ListNode l0 = new ListNode(9);
		ListNode l1 = new ListNode(1);
		ListNode l2 = new ListNode(9);
		ListNode l3 = new ListNode(9);
		ListNode l4 = new ListNode(9);
		ListNode l5 = new ListNode(9);
		ListNode l6 = new ListNode(9);
		ListNode l7 = new ListNode(9);
		ListNode l8 = new ListNode(9);
		ListNode l9 = new ListNode(9);
		l1.next=l2;
		l2.next=l3;
		l3.next=l4;
		l4.next=l5;
		l5.next=l6;
		l6.next=l7;
		l7.next=l8;
		l8.next=l9;
		l9.next=null;
		l0.next=null;
		
		l = addTwoNumbers(l0,l1);
		for(p=l;p!=null;p=p.next)
		System.out.println(p.val);
	}
}



歡迎指正。轉載請申明:http://blog.csdn.net/u010498696/article/details/46051617

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章