Add Two Number

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

两个链表代表的两个数字相加,逆向表示。

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution:
 8     # @param {ListNode} l1
 9     # @param {ListNode} l2
10     # @return {ListNode}
11     def addTwoNumbers(self, l1, l2):
12         if l1==None:return l2                     #在最开始的时候,其中有任意的值为空,即个位数都为空,相加直接返回另一个数
13         if l2==None:return l1
14         dummy=ListNode(0)
15         p=dummy
16         flag=0                                    #对进位的计数
17         while l1 and l2:
18             p.next=ListNode((l1.val+l2.val+flag)%10)
19             flag=(l1.val+l2.val+flag)/10
20             l1=l1.next;l2=l2.next;p=p.next
21         if l1:                                    #当前面的while l1和l2每一位都不为空结束时,若l1较长还有高数位的数,l2已为空时
22             while l1:
23                 p.next=ListNode((l1.val+flag)%10)
24                 flag=(l1.val+flag)/10
25                 l1=l1.next;p=p.next
26         if l2:                                    #l2比l1位数更多时
27             while l2:
28                 p.next=ListNode((l2.val+flag)%10)
29                 flag=(l2.val+flag)/10
30                 l2=l2.next;p=p.next
31         if flag==1:                               #最终还有一个进位时
32             p.next=ListNode(1)
33         return dummy.next
时间: 2024-10-27 11:17:32

Add Two Number的相关文章

Interview - Add two number - #2

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 #2 Add two number

Q: 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 merge list and add two number,ismatch

preview: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples:

LeetCode——Largest Number

Description: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string in

hdu 6231 -- K-th Number(二分+尺取)

题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an array B by a parameter K as following rules: Initially, the array B is empty. Consider each interval in array A. If the length of this interval is le

LeetCode-Medium-Add Two Number

###Add Two Number本来是一道非常简单的题目原题目的意思是,给两个链表,两个链表都链尾是十进制的头部例如有链表形式 9->8->7 对应的十进制就是789,而后的操作时将两个链表相加并且合并到一个链表里面 ###第一次的解法我第一次是被结构体卡住了,才考研完,很多基础知识全忘了,比如构造函数,与函数的返回值以及传参的问题,导致自己被这个结构体卡了半天,所以第一个解法就没有用leetcode给的结构体,而是自己的结构体,显然是不会通过leetcode只是为了自己理清思路.```cp

3、创建数据库、表空间、权限管理、表、约束、序列

一.dos常用命令 右键→标记→选中要复制的内容→右击就可以完成赋值 ↑表示找前面代码 data 查看日志time 查看时间cls 清屏exit 退出regedit 注册表taskmgr 任务管理器compmgmt.msc计算机管理mspaint 画图板 开始 运行 命令 集锦 --------------------------------write----------写字板 notepad--------打开记事本shrpubw--------创建共享文件夹 calc-----------启

Oracle数据库语句大全

转自:http://blog.sina.com.cn/s/blog_b5d14e2a0101c56z.html ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CHECK (检查)--检查在约束中指定的条件是否得到了满足. UNIQUE (唯一)--保证在指定的列中没有重复值.在该表中每一个值或者每一组值都将是唯一的. PRIMARY KEY (主键)--用来唯一的标识出表的每

【TypeScript】TypeScript 学习 5——方法

在 JavaScript 中,有两种方式定义方法. 1.命名的方法 function add(x,y){ return x+y; } 2.匿名方法 var myAdd = function(x,y) { return x+y;}; 在 TypeScript 中,也兼容上面两种定义方式,但是,既然我们用的是 TypeScript,那么肯定要强于本来的定义方式. 1.类型化方法 function add(x:number, y:number):number{ return x+y; } var my