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

AC代码如下:

ListNode* add(ListNode* l1, ListNode* l2,int len1,int len2){
    if(len1 <len2)
        return add(l2,l1,len2,len1);
    //len1>=len2;
    ListNode* tmp1=l1;
    ListNode* tmp2=l2;
    int jin=0;
    while(tmp2!=NULL)
    {
        int now = (tmp1->val+tmp2->val)+jin;
        if(now>=10)
        {
            jin = now/10;
            now = now-10;
        }
        else
        {
            jin= 0;
        }
        tmp1->val = now;
        tmp1=tmp1->next;
        tmp2=tmp2->next;
    }
    for(;jin>0,tmp1!=NULL;tmp1=tmp1->next)
    {
        int now = jin + tmp1->val;
        if(now >= 10)
        {
            jin = now/10;
            now = now-10;
        }
        else
        {
            jin=0;
        }
        tmp1->val = now;

    }
    if(tmp1==NULL&&jin>0)
    {
        //ListNode node(jin);
        ListNode* nodenew = new ListNode(jin);
        ListNode *tmp=l1;
        for(;tmp->next!=NULL;tmp=tmp->next)
            ;
        tmp->next = nodenew;
    }
    return l1;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    if(l1 == NULL && l2 == NULL)
        return NULL;
    else if(l1 == NULL)
        return l2;
    else if(l2 == NULL)
        return l1;
    else
    {
        int len1=0,len2=0;
        ListNode* tmp;
        for(tmp=l1;tmp!=NULL;tmp=tmp->next)
        {
            len1++;
        }
        for(tmp=l2;tmp!=NULL;tmp=tmp->next)
        {
            len2++;
        }
        return add(l1,l2,len1,len2);
    }
}

别人写的,也差不多。主要需要考虑进位的事情。有多种情况。两个链表长度相等时,只要最后有进位,就直接放到后面。如果不等,则把长度小的数加到长度较大的数上,然后判断进位。

时间: 2024-10-12 16:15:23

LeetCode【2】Add two numbers的相关文章

LeetCode【2】. Add Two Numbers--java实现

第二道题 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.     Inpu

【链表】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【67】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 看起来挺好写的样子,没想到墨迹了半天. string add(string a, string b,int lena,int lenb) { if(lena<lenb) return add(b,a,lenb,lena); //

【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刷题笔记】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】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 字符串处理】Compare Version Numbers

[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume

Leetcode 线性表 数 Add Two Numbers

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Add Two Numbers Total Accepted: 13127 Total Submissions: 58280 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes

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