首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长。

 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
int g,s=,stmp;
struct ListNode *pl,*ptmp,*pi;
pl=NULL;
while(l1&&l2)
{
g=l1->val+l2->val;
g+=s;
stmp=g/;
g%=;
struct ListNode* ptmp=(struct ListNode *)malloc(sizeof(struct ListNode));
ptmp->next=NULL;
ptmp->val=g;
if(pl==NULL)
{
pl=ptmp;
pi=pl;
}
else
{
pi->next=ptmp;
pi=pi->next;
}
s=stmp;
l1=l1->next;
l2=l2->next;
}
if(l1==NULL)
l1=l2;
while(l1)
{
g=l1->val;
g+=s;
stmp=g/;
g%=;
struct ListNode *ptmp=malloc(sizeof(struct ListNode));
ptmp->next=NULL;
ptmp->val=g;
pi->next=ptmp;
pi=pi->next;
s=stmp;
l1=l1->next;
}
if(s)
{
struct ListNode *ptmp=malloc(sizeof(struct ListNode));
ptmp->next=NULL;
ptmp->val=s;
pi->next=ptmp;
}
return pl;
}

Python:

 # Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
tmp = l1
up = 0
while l1 and l2:
l1.val += (l2.val+up)
if l1.val > 9:
up = 1
l1.val %= 10
else:
up = 0
p1,p2 = l1,l2
l1,l2 = l1.next,l2.next
if not l1 and not l2:
if up:
p1.next = ListNode(up)
return tmp
if l2:
p1.next = l2
l1 = p1.next
while l1 and up:
l1.val += up
if l1.val >9:
up=1
l1.val %= 10
else:
up = 0
p1 = l1
l1 = l1.next
if up:
p1.next = ListNode(up)
return tmp

【LeetCode】2.Add Two Numbers的更多相关文章

  1. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  2. 【Leetcode】445. Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  3. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  4. 【LeetCode】2.Add Two Numbers 链表数相加

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  5. 【LeetCode】2. Add Two Numbers 两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  6. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  8. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

  9. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

随机推荐

  1. python绝技 — 搜寻蓝牙设备

    需要安装蓝牙模块:pybluez sudo pip install pybluez 代码 #!/usr/bin/env python #--*--coding=utf-8--*-- #P191 #su ...

  2. 批处理+组策略 实现规定时间段无法开机and定时关机

    某爱熬夜的人对付自己的东西 1.shutdown命令 shutdown -a #取消现有的shutdown计划 shutdown -s -t [time] #设定时间关机 shutdown -r -t ...

  3. Fatal error: Call to undefined function oci_connect()

    http://stackoverflow.com/questions/22478387/call-to-undefined-function-oci-connect Whenever you conn ...

  4. Linux下制作静(动)态库

    关键命令: 动态库制作命令 gcc xxx.c -fPIC -shared -o libxxx.so 静态库制作命令 gcc -c xxx.c ar crv libxxx.a xxx.o 例: //h ...

  5. Photoshop定义画笔选区为空的原因

    定义画笔预设时,选择选区后需填充黑色,否则将出现选区为空的提示

  6. [Q]pdfFactory打印机纸张方向设置为横向

    不推荐更改pdfFactory打印机默认纸张方向(默认为横向),更改后可能导致不必要的麻烦(pdfFactory要求所定义的纸张方向与实际的纸张方向需一致,因此若更改为横向,则纸张宽度的定义需大于纸张 ...

  7. 用CKEDITOR 做自助上传的解决方案2

    1,在plugins下新建文件夹 multiimg 2,创建文件plugin.js (function() { CKEDITOR.plugins.add("multiimg", { ...

  8. emguCv3.x 实现字符分割,轮廓检测

    /// <summary> /// 获取区域 /// </summary> /// <param name="bitmap"></para ...

  9. C#第十天

    1.c#中的访问修饰符 public :公开的公共的 private:私有的,只能在当前类的内部访问 protected:受保护的,只能在当前类的内部以及该类的子类中访问. internal:只能在当 ...

  10. winform窗体对象 单例模式与泛型结合

    实现弹出窗体对象的单例模式  结合泛型后,可以用于所有窗体的弹出操作 public class BaseFrm<T> where T : Form, new() { //定义一个静态的,私 ...