所思 所学 我写 我得

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,若进位必为1p->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+1if(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);}}

欢迎指正。转载请申明:

肯承认错误则错已改了一半

所思 所学 我写 我得

相关文章:

你感兴趣的文章:

标签云: