博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Add Two Numbers
阅读量:6989 次
发布时间:2019-06-27

本文共 1076 字,大约阅读时间需要 3 分钟。

问题描述:

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

代码:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {    struct ListNode* res, *L1=l1, *L2=l2, *temp;    while (L1&&L2) {        L1->val=L2->val=L1->val+L2->val;        L1 = L1->next;        L2 = L2->next;    }    res = (L1) ? l1 : l2;    temp=res;    while (temp) {        if (temp->val>=10) {            if (!temp->next)             temp->next = (struct ListNode*)calloc(1,sizeof(struct ListNode));            temp->val-=10;            temp->next->val+=1;        }         temp = temp->next;    }    return res;}

转载于:https://www.cnblogs.com/chxuan/p/8232113.html

你可能感兴趣的文章
I.MX6 I2C DS1337 disable square-wave output
查看>>
php中一些函数的用法
查看>>
【BZOJ】3996: [TJOI2015]线性代数
查看>>
巧用枚举类型,实现项目的多语言切换
查看>>
Hibernate createCriteria查询详解
查看>>
关于Action返回结果类型的事儿(下)
查看>>
检测客户端显示器分辨率、浏览器类型和客户端IP
查看>>
Thread之三:Thread Join()的用法
查看>>
C编程基础
查看>>
jquery判断滚动条是否到底部
查看>>
jquery 选择对象随心所欲,遍历数组更是易如反掌
查看>>
CI-持续集成(1)-软件工业“流水线”概述
查看>>
JSF教程(9)——生命周期之Process Validations Phase
查看>>
[转载]AxureRP常用快捷键
查看>>
【zookeeper】 zookeeper 集群搭建
查看>>
OpenStack 中的neutron-server启动过程
查看>>
Java Runtime.availableProcessors()方法
查看>>
Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号...
查看>>
busybox filesystem udhcpc 原理
查看>>
OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决
查看>>