LeetCode算法題——Add Two Numbers

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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

算法思想:創建鏈表,逐位相加,逢十進一,保存進位,加到下一位中運算
C++代碼如下:
#include <iostream>
#include "stdlib.h"
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) {
        int v1,v2,j=0,v;
        ListNode *head,*p1,*p2;
        head=p1=NULL;
        while(l1!=NULL||l2!=NULL){
            if(l1!=NULL){
                v1=l1->val;
                l1=l1->next;
            }else{
                v1=0;
            }
            if(l2!=NULL){
                v2=l2->val;
                l2=l2->next;
            }else{
                v2=0;
            }
            if(v1+v2+j<10){
                v=v1+v2+j;
                j=0;
            }else{
                v=v1+v2+j-10;
                j=1;
            }
            p2=(ListNode *)malloc(sizeof(ListNode));
            p2->val=v;
            p2->next=NULL;
            if(head==NULL){
                head=p1=p2;
            }else{
                p1->next=p2;
                p1=p1->next;
            }
        }
        if(j==1){
            p2=(ListNode *)malloc(sizeof(ListNode));
            p2->val=1;
            p2->next=NULL;   
            if(head==NULL){
                head=p1=p2;
            }else{
                p1->next=p2;
                p1=p1->next;
            }
        }
        return head;
    }
};

int main(){
    ListNode *l1h,*p1,*p2,*l2h,*q1,*q2,*res;
    p1=NULL;
    l1h=NULL;
    q1=NULL;
    l2h=NULL;
    int n,t,m,s;
    cout<<"please input the first List element number:";
    cin>>n;
    for(int i=0;i<n;i++){
        p2=(ListNode *)malloc(sizeof(ListNode));
        cin>>t;
        p2->val=t;
        p2->next=NULL;
        if(l1h==NULL){
            l1h=p1=p2;
        }else{
            p1->next=p2;
            p1=p1->next;
        }               
    }

    cout<<"please input the second List element number:";
    cin>>m;
    for(int i=0;i<m;i++){
        q2=(ListNode *)malloc(sizeof(ListNode));
        cin>>s;
        q2->val=s;
        q2->next=NULL;
        if(l2h==NULL){
            l2h=q1=q2;
        }else{
            q1->next=q2;
            q1=q1->next;
        }               
    }
//    while(l1h!=NULL){
//        cout<<l1h->val<<" ";
//        l1h=l1h->next;
//    }
//    cout<<endl;
//    while(l2h!=NULL){
//        cout<<l2h->val<<" ";
//        l2h=l2h->next;
//    }
    Solution sol;
    res=sol.addTwoNumbers(l1h,l2h);
    while(res!=NULL){
        cout<<res->val<<" ";
        res=res->next;
    }

}
發佈了34 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章