Add Two Numbers(转)
- LeetCode之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在链表中为2->4->3。链表每一个节点包含一个数字(0-9)。
计算这两个数字和并以链表形式返回。
【分析】
无
【代码1】
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112/********************************** 日期:2014-01-27* 作者:SJF0115* 题号: Add Two Numbers* 结果:AC* 来源:LeetCode* 总结:**********************************/#include <iostream>#include <stdio.h>#includeusing namespace std;struct ListNode {intval;ListNode *next;ListNode(intx) : val(x), next(NULL) {}};classSolution {public:ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {ListNode *head = (ListNode *)malloc(sizeof(ListNode));ListNode *pre = head;ListNode *node = NULL;//进位intc =0,sum;//加法while(l1 != NULL && l2 != NULL){sum = l1->val + l2->val + c;c = sum /10;node = (ListNode *)malloc(sizeof(ListNode));node->val = sum %10;node->next = NULL;//尾插法pre->next = node;pre = node;l1 = l1->next;l2 = l2->next;}//例如:2->4->3->1 5->6->4while(l1 != NULL){sum = l1->val + c;c = sum /10;node = (ListNode *)malloc(sizeof(ListNode));node->val = sum %10;node->next = NULL;//尾插法pre->next = node;pre = node;l1 = l1->next;}//例如:2->4->3 5->6->4->1while(l2 != NULL){sum = l2->val + c;c = sum /10;node = (ListNode *)malloc(sizeof(ListNode));node->val = sum %10;node->next = NULL;//尾插法pre->next = node;pre = node;l2 = l2->next;}//最后一位还有进位if(c >0){node = (ListNode *)malloc(sizeof(ListNode));node->val = c;node->next = NULL;//尾插法pre->next = node;pre = node;}returnhead->next;}};intmain() {Solution solution;intA[] = {2,4,7,9};intB[] = {5,6,4};ListNode *head = NULL;ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));head1->next = NULL;head2->next = NULL;ListNode *node;ListNode *pre = head1;for(inti =0;i <4;i++){node = (ListNode*)malloc(sizeof(ListNode));node->val = A[i];node->next = NULL;pre->next = node;pre = node;}pre = head2;for(inti =0;i <3;i++){node = (ListNode*)malloc(sizeof(ListNode));node->val = B[i];node->next = NULL;pre->next = node;pre = node;}head = solution.addTwoNumbers(head1->next,head2->next);while(head != NULL){printf("%d ",head->val);head = head->next;}return0;}</algorithm></stdio.h></iostream>
【代码2】
1234567891011121314151617181920212223242526classSolution {public:ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {ListNode *head = (ListNode *)malloc(sizeof(ListNode));ListNode *pre = head;ListNode *node = NULL;//进位intc =0,sum,val1,val2;//加法while(l1 != NULL || l2 != NULL || c !=0){val1 = (l1 == NULL ?0: l1->val);val2 = (l2 == NULL ?0: l2->val);sum = val1 + val2 + c;c = sum /10;node = (ListNode *)malloc(sizeof(ListNode));node->val = sum %10;node->next = NULL;//尾插法pre->next = node;pre = node;l1 = (l1 == NULL ? NULL : l1->next);l2 = (l2 == NULL ? NULL : l2->next);}returnhead->next;}};

浙公网安备 33010602011771号