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 -> 4)
Output: 7 -> 0 -> 8

题目比较简单, 主要考察链表操作的熟练程度, 以及各种边界情况的处理, 这种题目, 要一次写对, 不留 bug.

 1 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 2         if (l1 == null) return l2;
 3         if (l2 == null) return l1;
 4
 5         ListNode head = new ListNode(0);
 6         ListNode curr = head;
 7         boolean isCarry = false;
 8         while (l1 != null || l2 != null) {
 9             int val = (isCarry ? 1 : 0) + (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val);
10             if (val >= 10) {
11                 isCarry = true;
12                 val -= 10;
13             } else {
14                 isCarry = false;
15             }
16             ListNode node = new ListNode(val);
17             curr.next = node;
18             curr = node;
19             l1 = l1 == null ? null : l1.next;
20             l2 = l2 == null ? null : l2.next;
21         }
22
23         if (isCarry) {
24             ListNode node = new ListNode(1);
25             curr.next = node;
26         }
27
28         return head.next;
29     }
时间: 2024-10-24 04:25:45

Interview - Add two number - #2的相关文章

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

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