[LeetCode]2.Add Two Numbers

来源:互联网 发布:linux usermod ag 编辑:程序博客网 时间:2024/04/28 09:52

【题目】

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】

/**********************************   日期:2014-01-27*   作者:SJF0115*   题目: 2.Add Two Numbers*   网址:http://oj.leetcode.com/problems/add-two-numbers/*   结果:AC*   来源:LeetCode*   总结:**********************************/#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;struct ListNode {    int val;    ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};class Solution {public:    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {        ListNode *head = (ListNode *)malloc(sizeof(ListNode));        ListNode *pre = head;        ListNode *node = NULL;        //进位        int c = 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->4        while(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->1        while(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;        }        return head->next;    }};int main() {    Solution solution;    int A[] = {2,4,7,9};    int B[] = {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(int i = 0;i < 4;i++){        node = (ListNode*)malloc(sizeof(ListNode));        node->val = A[i];        node->next = NULL;        pre->next = node;        pre = node;    }    pre = head2;    for(int i = 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;    }    return 0;}

【代码2】

class Solution {public:    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {        ListNode *head = (ListNode *)malloc(sizeof(ListNode));        ListNode *pre = head;        ListNode *node = NULL;        //进位        int c = 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);        }        return head->next;    }};


1 0
原创粉丝点击