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 -> 4)
Output: 7 -> 0 -> 8
题意解析:
给定两个链表表示两个非负数。数字逆序存储,每一个节点包括一个单一的数字。计算两个链表表示的数的和。并以相同格式的链表的形式返还结果。
解法就是直接操作链表相加就能够了。。
代码例如以下:
C++
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode * ans = NULL, *last = NULL;
int up = 0;
while (NULL != l1 && NULL != l2) {
int tmp = l1->val + l2->val + up;
up = tmp / 10;
if (NULL == last) {
ans = new ListNode(tmp % 10);
last = ans;
} else
last = pushBack(last, tmp % 10);
l1 = l1->next;
l2 = l2->next;
}
while (NULL != l1) {
int tmp = l1->val + up;
last = pushBack(last, tmp % 10);
up = tmp / 10;
l1 = l1->next;
}
while (NULL != l2) {
int tmp = l2->val + up;
last = pushBack(last, tmp % 10);
up = tmp / 10;
l2 = l2->next;
}
if (0 != up) {
ListNode * l = new ListNode(up);
last->next = l;
}
return ans;
} ListNode * pushBack(ListNode * last, int val) {
ListNode * l = new ListNode(val);
last->next = l;
return l;
}
};
Python
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
carry = 0; head = ListNode(0); curr = head;
while l1 and l2:
Sum = l1.val + l2.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l1 = l1.next; l2 = l2.next; curr = curr.next
while l1:
Sum = l1.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l1 = l1.next; curr = curr.next
while l2:
Sum = l2.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l2 = l2.next; curr = curr.next
if carry > 0:
curr.next = ListNode(carry)
return head.next
水一枚。。。。
LeetCode第四题,Add Two Numbers的更多相关文章
- leetcode第四题--Add Two Numbers
Problem: You are given two linked lists representing two non-negative numbers. The digits are stored ...
- Leetcode 第 2 题(Add Two Numbers)
Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode解题笔记 - 2. Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode第二题—— Add Two Numbers(模拟两数相加)
Description: You are given two non-empty linked lists representing two non-negative integers. The di ...
- [算法题] Add Two Numbers
题目内容 题目来源:LeetCode You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
随机推荐
- Oracle EBS-SQL (WIP-13):检查任务组件未选MRP净值.sql
select WE.WIP_ENTITY_NAME 任务号, MFG_LOOKUPS_WJS. ...
- JVM内存分配和回收
本文内容来自<Java编程思想(第四版)>第二章<一切都是对象>和第五章<初始化与清理>.作为一个使用了好几年的Javaer,再次看编程思想的前面章节(不要问我为什 ...
- Delphi SysErrorMessage 函数和系统错误信息表
在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMes ...
- C语言malloc和free实现原理
以下是一段简单的C代码,malloc和free到底做了什么? int main() { char* p = (char*)malloc(32); free(p); return 0; } malloc ...
- sctf pwn300
拿到程序后,拉入IDA,大概看了一番后,尝试运行,进一步了解程序的功能. 发现NX enabled,No PIE. 一号是一个猜数字的游戏,二号是一个留言本,三号是打印出留言的内容,四号是退出. 观察 ...
- vs2010:【“System.Data.OracleClient.OracleConnection”已过时】警告
在oracle 安装目录下 找到 Oracle.DataAccess.dll添加引用,然后 using Oracle.DataAccess.Client;其他的都不用动,即可.连接字符串中 如有 用的 ...
- SharePoint 2013 强制安装解决方案
Add-SPSolution Install-SPSolution -Identity DemonstrationZone.wsp -GACDeployment -CompatibilityLevel ...
- list去重 转载
http://blog.csdn.net/huaishuming/article/details/47778319 1. 单个List 去重: 如果用的是Set集合就不用怕重复的问题了,如果用的Lis ...
- php mysql_insert_id() 获取为空
mysql_insert_id() 获取插入数据后的最新的id 遇到问题和解决的步骤如下: 1. 使用后总是返回空的字符串,网上查了一番有人说是id要AUTO_INCREMENT,并且mysql_in ...
- 加密传输SSL协议8_Apache服务器的安装
学习了那么多的理论的知识,下面通过在Apache服务器中安装和使用SSL协议,实现安全传输,但是首先要安装好Apache服务器. Apache服务器的安装 Linux下所有的软件的原码的安装都是三部曲 ...