167. Add Two Numbers【easy】
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
Given 7->1->6 + 5->9->2. That is, 617 + 295.
Return 2->1->9. That is 912.
Given 3->1->5 and 5->9->2, return 8->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 addLists(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null) {
return null;
} ListNode head = new ListNode(0);
ListNode point = head;
int carry = 0;
while(l1 != null && l2!=null){
int sum = carry + l1.val + l2.val;
point.next = new ListNode(sum % 10);
carry = sum / 10;
l1 = l1.next;
l2 = l2.next;
point = point.next;
} while(l1 != null) {
int sum = carry + l1.val;
point.next = new ListNode(sum % 10);
carry = sum /10;
l1 = l1.next;
point = point.next;
} while(l2 != null) {
int sum = carry + l2.val;
point.next = new ListNode(sum % 10);
carry = sum /10;
l2 = l2.next;
point = point.next;
} if (carry != 0) {
point.next = new ListNode(carry);
}
return head.next;
}
}
中规中矩的解法
解法二:
public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
int carry = 0;
for (ListNode i = l1, j = l2; i != null || j != null; ) {
int sum = carry;
sum += (i != null) ? i.val : 0;
sum += (j != null) ? j.val : 0;
tail.next = new ListNode(sum % 10);
tail = tail.next;
carry = sum / 10;
i = (i == null) ? i : i.next;
j = (j == null) ? j : j.next;
}
if (carry != 0) {
tail.next = new ListNode(carry);
}
return dummy.next;
}
}
比较简明的写法,且使用了dummy节点,参考@NineChapter 的代码
解法三:
public class Solution {
/*
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists(ListNode l1, ListNode l2) {
ListNode root = new ListNode(-1);
ListNode result = root;
int carry = 0;
while( l1 != null || l2 != null || carry == 1){
int value = 0;
if(l1 != null){
value += l1.val;
l1 = l1.next;
}
if( l2 != null){
value += l2.val;
l2 = l2.next;
}
value += carry;
root.next = new ListNode(value % 10);
carry = value / 10;
root = root.next;
}
return result.next;
}
}
解法四:
class Solution {
public:
ListNode* addLists(ListNode* l1, ListNode* l2) {
ListNode *head = new ListNode();
ListNode *ptr = head;
int carry = ;
while (true) {
if (l1 != NULL) {
carry += l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
carry += l2->val;
l2 = l2->next;
}
ptr->val = carry % ;
carry /= ;
// 当两个表非空或者仍有进位时需要继续运算,否则退出循环
if (l1 != NULL || l2 != NULL || carry != ) {
ptr = (ptr->next = new ListNode());
} else {
break;
}
}
return head;
}
};
非常简明的代码,参考@NineChapter 的代码
167. Add Two Numbers【easy】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 167. Add Two Numbers【LintCode by java】
Description You have two numbers represented by a linked list, where each node contains a single dig ...
- 633. Sum of Square Numbers【Easy】【双指针-是否存在两个数的平方和等于给定目标值】
Given a non-negative integer c, your task is to decide whether there're two integers a and bsuch tha ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 448. Find All Numbers Disappeared in an Array【easy】
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
随机推荐
- html端编码规范
理想的方式是让HTML只用于定义内容呈现的结构,让CSS控制内容呈现的样式,而所有功能的实现定义在JavaScript中
- Flume目录
1. Flume的安装配置 2. flume和kafka整合(转) 3. Flume NG 配置详解(转) 4. Flume-NG一些注意事项(转) 5. FLume监控文件夹,将数据发送给Kafka ...
- 值得阅读的C语言开源项目代码
本文地址:http://www.cnblogs.com/archimedes/p/c-opensource-project.html,转载请注明源地址. 本篇文章主要总结一些C开源项目,有些是很著名的 ...
- Java GC、新生代、老年代
堆内存 Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象.在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ( ...
- VMWARE虚拟机安装64位系统此主机支持IntelVTx 但IntelVTx处于禁用状态
1.进入BIOS(我的电脑是Thinkpad e480,进入按钮是F12/ Fn+F12) 2.选择App Menu,再选择第一项Setup,进入 3.选择Security,选择下面第四项Virtua ...
- 免费的.net云平台appharbor.com
支持Mysql,MSSQL2008R2免费的都是20MB 提供codeplex,GitHub,BitBucket部署到appharbor.com 也可以使用Git来部署应用,但不支持FTP 可以一键安 ...
- Mysql创建、删除用户、查询所有用户等教程,提升您的MYSQL安全度!
建议您不要使用mysql的root账号来为您的web页面连接数据库,这可能会导致用户通过网页获取到您的数据库账号密码,存在严重的安全隐患. 建议新建一账号,权限设置基本够用,然后使用那新建的账号来连接 ...
- CSS中的margin、border和padding的区别
aaarticlea/gif;base64,R0lGODlhuQEbAbMAAP8AM8zMzGZmYszMmZmZZkIP/5qE/8zM/wICApmZmf//zP///wAAAAAAAAAAAA
- QtGui.QGridLayout
The most universal layout class is the grid layout. This layout divides the space into rows and colu ...
- SyntaxError: Non-UTF-8 code starting with '\xc5' in file t.py on line 3,but no encoding declared;see http://python.org/dev/peps/pep-0263/ for details
解决方案是: 在程序最上面加上:# coding=gbk 这样程序就可以正常运行了.