[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

【分析】思路非常简单,利用两个指针分别遍历两个链表,并且用一个变量表示是否有进位。某个链表遍历结束之后再将另一个链表连接在结果链表之后即可,若最后有进位需要添加一位。

struct ListNode* addTwoNumbers(struct ListNode* FirstLists,
                                    struct ListNode* SecondLists)
{
    /*1.异常处理*/
    if(!FirstLists && !SecondLists)
        return NULL;

    struct ListNode *FirstListsTemp = FirstLists;
    struct ListNode *SecondListsTemp = SecondLists;
    struct ListNode *head = NULL;
    struct ListNode *headTemp = NULL;

    int TwoNumbersSum = 0;
    int TwoNumbersGain = 0; 

    while(FirstLists && SecondLists) {
        TwoNumbersSum = FirstLists->val + SecondLists->val + TwoNumbersGain;
        TwoNumbersGain = TwoNumbersSum/10;
        TwoNumbersSum = TwoNumbersSum%10;
        /*2.保存头结点*/
        if (NULL == head) {
            head = (struct ListNode *)malloc(sizeof(struct ListNode));
            head->val = TwoNumbersSum;
            headTemp = head;
            headTemp->next = NULL;
        }
        else {
            headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
            headTemp = headTemp->next;
            headTemp->val = TwoNumbersSum;
            headTemp->next = NULL;
        }
        FirstLists = FirstLists->next;
        SecondLists = SecondLists->next;
    }

    while(FirstLists) {
        TwoNumbersSum = FirstLists->val + TwoNumbersGain;
        TwoNumbersGain = TwoNumbersSum/10;
        TwoNumbersSum = TwoNumbersSum%10;

        if (NULL == head) {
            head = (struct ListNode *)malloc(sizeof(struct ListNode));
            head->val = TwoNumbersSum;
            headTemp = head;
            headTemp->next = NULL;
        }
        else {
            headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
            headTemp = headTemp->next;
            headTemp->val = TwoNumbersSum;
            headTemp->next = NULL;
        }
        FirstLists = FirstLists->next;
    }

    while(SecondLists) {
        TwoNumbersSum = SecondLists->val + TwoNumbersGain;
        TwoNumbersGain = TwoNumbersSum/10;
        TwoNumbersSum =   TwoNumbersSum%10;

        if (NULL == head) {
            head = (struct ListNode *)malloc(sizeof(struct ListNode));
            head->val = TwoNumbersSum;
            headTemp = head;
            headTemp->next = NULL;
        }
        else {
            headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
            headTemp = headTemp->next;
            headTemp->val = TwoNumbersSum;
            headTemp->next = NULL;
        }
        SecondLists = SecondLists->next;
    }

    if(TwoNumbersGain) {
        headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
        headTemp = headTemp->next;
        headTemp->val = TwoNumbersGain%10;
        headTemp->next = NULL;
    }

    return head;
}

完整代码如下:

// addTwoNumbers.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
 * Definition for singly-linked list.
 **/

 struct ListNode {
      int val;
      struct ListNode *next;
 };

 struct ListNode* CreatLink(int aData[],int len)
 {
     if(!aData)
         return NULL;
     struct ListNode *head = NULL;
     struct ListNode *p = NULL;
     struct ListNode *Link= NULL;
     /*第一个节点*/
     head = (struct ListNode *)malloc(sizeof(struct ListNode));
     head->val = aData[0];
     head->next = NULL;
    // aData ++;
     Link = head; /*保存头结点*/
     int i = 1;

     while(i < len) {
         p = (struct ListNode *)malloc(sizeof(struct ListNode));
         p->val = aData[i];
         head->next = p;
         head = p;
         i ++;
     }

     head->next = NULL;
     return Link;
 }

 void printLink(struct ListNode *head)
 {
     while(head) {
         printf("%d",head->val);
         head = head->next;
     }
     printf("\n");
 }

struct ListNode* addTwoNumbers(struct ListNode* FirstLists,
                                    struct ListNode* SecondLists)
{
    /*1.异常处理*/
    if(!FirstLists && !SecondLists)
        return NULL;

    struct ListNode *FirstListsTemp = FirstLists;
    struct ListNode *SecondListsTemp = SecondLists;
    struct ListNode *head = NULL;
    struct ListNode *headTemp = NULL;

    int TwoNumbersSum = 0;
    int TwoNumbersGain = 0; 

    while(FirstLists && SecondLists) {
        TwoNumbersSum = FirstLists->val + SecondLists->val + TwoNumbersGain;
        TwoNumbersGain = TwoNumbersSum/10;
        TwoNumbersSum = TwoNumbersSum%10;
        /*2.保存头结点*/
        if (NULL == head) {
            head = (struct ListNode *)malloc(sizeof(struct ListNode));
            head->val = TwoNumbersSum;
            headTemp = head;
            headTemp->next = NULL;
        }
        else {
            headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
            headTemp = headTemp->next;
            headTemp->val = TwoNumbersSum;
            headTemp->next = NULL;
        }
        FirstLists = FirstLists->next;
        SecondLists = SecondLists->next;
    }

    while(FirstLists) {
        TwoNumbersSum = FirstLists->val + TwoNumbersGain;
        TwoNumbersGain = TwoNumbersSum/10;
        TwoNumbersSum = TwoNumbersSum%10;

        if (NULL == head) {
            head = (struct ListNode *)malloc(sizeof(struct ListNode));
            head->val = TwoNumbersSum;
            headTemp = head;
            headTemp->next = NULL;
        }
        else {
            headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
            headTemp = headTemp->next;
            headTemp->val = TwoNumbersSum;
            headTemp->next = NULL;
        }
        FirstLists = FirstLists->next;
    }

    while(SecondLists) {
        TwoNumbersSum = SecondLists->val + TwoNumbersGain;
        TwoNumbersGain = TwoNumbersSum/10;
        TwoNumbersSum =   TwoNumbersSum%10;

        if (NULL == head) {
            head = (struct ListNode *)malloc(sizeof(struct ListNode));
            head->val = TwoNumbersSum;
            headTemp = head;
            headTemp->next = NULL;
        }
        else {
            headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
            headTemp = headTemp->next;
            headTemp->val = TwoNumbersSum;
            headTemp->next = NULL;
        }
        SecondLists = SecondLists->next;
    }

    if(TwoNumbersGain) {
        headTemp->next = (struct ListNode *)malloc(sizeof(struct ListNode));
        headTemp = headTemp->next;
        headTemp->val = TwoNumbersGain%10;
        headTemp->next = NULL;
    }

    return head;
}

int main()
{
    int a[3] = {2,4,3};
    int b[3] = {5,6,4};
    struct ListNode *head1 = NULL;
    struct ListNode *head2 = NULL;
    struct ListNode *head3 = NULL;
    head1 = CreatLink(a,3);
    printLink(head1);

    head2 = CreatLink(b,3);
    printLink(head2);

    head3 = addTwoNumbers(head1,head2);
    printLink(head3);
    /*To Do:释放内存*/
    getchar();
    return 0;

}
时间: 2024-10-03 07:42:22

[LeetCode-2] Add Two Numbers(链表数据之和)的相关文章

leetcode 2 Add Two Numbers(链表)

数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListN

[LeetCode]2. Add Two Numbers链表相加

注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode res = null; ListNode temp = null; //注意如果进位不是0的话还要添加一个节点 while (l1!=null||l2!=null||flag!=0) { int cur = flag; flag = 0; if (l1!=null) { cur+=l1.val; l

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文: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

LeetCode --- 2. Add Two Numbers

题目链接: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

【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 -&

leetcode -day20 Add Two Numbers

1.  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 ->

[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 -&g

【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 ->

LeetCode OJ - 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 -&

[C++]LeetCode: 108 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 -&