[Leet code 2]Two Sum
1 题目
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode headNode = new ListNode(0);
ListNode p = headNode;
int carry = 0; while (l1 != null && l2 != null) {
int var = l1.val + l2.val + carry;
carry = var / 10;
var = var % 10; ListNode node = new ListNode(var); p.next = node;
p = p.next; l1 = l1.next;
l2 = l2.next; }
//要考虑到前面可能有进位,要加上carry
while (l1 != null) {
int var = l1.val + carry;
carry = var /10;
var = var % 10;
ListNode node = new ListNode(var);
p.next = node;
p = p.next;
l1 = l1.next;
} while (l2 != null) {
int var = l2.val + carry;
carry = var /10;
var = var % 10;
ListNode node = new ListNode(var);
p.next = node;
p = p.next;
l2 = l2.next;
}
//要考虑到最后有个进位,就新建一个节点
if (carry != 0) {
ListNode node = new ListNode(carry);
p.next = node;
p = p.next;
}
return headNode.next;
}
}
[Leet code 2]Two Sum的更多相关文章
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- [Leet Code]Path Sum II
此题如果 #1 和 #4 判断分支交换,大集合就会超时(因为每次对于非叶子节点都要判断是不是叶子节点).可见,有时候if else判断语句也会对于运行时间有较大的影响. import java.uti ...
- [Leet Code]Path Sum
很简单一道题,搞错了N次,记录一下. public class Solution { private int currSum = 0; public boolean hasPathSum(TreeNo ...
- [原创]leet code - path sum
; ; ; } } ; }};
- #Leet Code# Divide Two Integers
描述:不使用 * / % 完成除法操作.O(n)复杂度会超时,需要O(lg(n))复杂度. 代码: class Solution: # @return an integer def dividePos ...
- #Leet Code# Root to leaf
语言:Python 描述:使用递归实现 def getList(self, node): if node is None: return [] if node.left is None and nod ...
- [leet code 4] Median of Two Sorted Arrays
1 题目 There are two sorted arrays A and B of size m and n respectively. Find the median of the two so ...
- Leet Code -- Unique BST
对于数字n(大于1).从1到n有多少种binary search tree(BST序列)?当n=3时,BST序列为: 1 3 3 2 1 \ ...
随机推荐
- [ ZooKeeper]ZooKeeper 的功能和原理
Zookeeper功能简介: ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现.分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅 ...
- 2019,UI设计师必备神器
2019年将会是你全新起航的一年,相信你已经制定了很多规划,正在开启第一步的推动. 作为对UI设计师更大程度的支持,今天特意为你分享一款释放你双手的设计神器.让你可以把时间和精力投入到设计本身,这 ...
- base64编码是什么1
首先明确一点base64 是一种编码格式.就想UNICODE一样,能在电脑上表示所有字符,或者换句话说通过编码能让电脑理解你想要表示的字符(因为电脑只知道0和1 ) 就像ascII 中 0100 00 ...
- python+selenium—webdriver入门(一)
一.浏览器最大化 二.设置浏览器分辨率大小 三.打印页面title 四.打印URL 五.控制浏览器前进或后退 #!/usr/bin/env python#-*- coding:utf-8 -*- fr ...
- (转贴)fusionCharts属性参考API
一.FusionCharts的分类 关于FusionCharts的基本介绍我就不在这里浪费篇幅了,想了解的朋友自己去www.baigoogedu.com里面找吧.我就说说FusionCharts的官方 ...
- inet_pton和inet_ntop inet_ntoa
Linux下地址转换函数 inet_pton ==> Ox inet_ntop ==> xxx.xxx.xxx #include <sys/types.h> #include ...
- 2018.12.31 bzoj3771: Triple(生成函数+fft+容斥原理)
传送门 生成函数经典题. 题意简述:给出nnn个数,可以从中选1/2/31/2/31/2/3个,问所有可能的和对应的方案数. 思路: 令A(x),B(x),C(x)A(x),B(x),C(x)A(x) ...
- OpenCV(1):显示图像
显示图像 #include<iostream> #include<opencv2/core/core.hpp> #include<opencv2/highgui/high ...
- CEdit控件[转]
1.CButton.CEdit等从CWnd继承了重要的功能: 使用CWnd::SetWindowText和CWnd::GetWindowText可以设置和获得窗口或控件上的文本.CWnd::SetFo ...
- springboot+cfx实现webservice功能
一.开发服务端 1.新建工程 cfx-webservice ,最终的完整工程如下: pom.xml如下: <?xml version="1.0" encoding=" ...