leetcode-2 Add Two Numbers 计算链表两个对应和的问题
2015-03-13 10:48
295 查看
1.问题描述:
You are given two linked lists representing two non-negativenumbers. The digits are stored in reverse order and each of their nodes containa single digit. Add
the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
http://www.programcreek.com/2012/12/add-two-numbers/
链表的定义:
/**
* 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) {
if((l1==null)||(l2==null))
{
return null;
}
int temp_val = l1.val + l2.val;
int go = temp_val /10;
ListNode result = new ListNode(temp_val%10);
l1 = l1.next;
l2 = l2.next;
ListNode temp = result;
while((l1!=null)&&(l2!=null))
{
temp_val = l1.val + l2.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l1 = l1.next;
l2 = l2.next;
go = temp_val /10;
}
while(l1!=null)
{
temp_val = l1.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l1 = l1.next;
go = temp_val /10;
}
while(l2!=null)
{
temp_val = l2.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l2 = l2.next;
go = temp_val /10;
}
if(go != 0)
{
temp.next = new ListNode(go);
}
return result;
}
}
解法二:较优的解法,但在leetcode上运行时间要劣于前者
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;//表示进位
ListNode head = new ListNode(0);//首前节点,参照C++的尾后节点
ListNode temp = head;
//一直循环至将两个链表遍历完全才退出
while((l1 != null)||(l2 != null))
{
//仅需确定和的几个因子大小即可;
int first = (l1 != null) ? l1.val : 0;//使用if语句亦可
int second = (l2 != null) ? l2.val : 0;
int result = first + second + carry;//每一次循环计算结果
carry = result /10;
ListNode pNode = new ListNode(result % 10);
temp.next = pNode;
temp = pNode;//准备下一循环
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
//还需考虑carray不等于0及仍有进位的情况
if (carry > 0)
{
temp.next = new ListNode(carry);
}
return head.next;//注意此返回值
}
}
1.问题描述:
You are given two linked lists representing two non-negativenumbers. The digits are stored in reverse order and each of their nodes containa single digit. Add
the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
http://www.programcreek.com/2012/12/add-two-numbers/
链表的定义:
/**
* 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) {
if((l1==null)||(l2==null))
{
return null;
}
int temp_val = l1.val + l2.val;
int go = temp_val /10;
ListNode result = new ListNode(temp_val%10);
l1 = l1.next;
l2 = l2.next;
ListNode temp = result;
while((l1!=null)&&(l2!=null))
{
temp_val = l1.val + l2.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l1 = l1.next;
l2 = l2.next;
go = temp_val /10;
}
while(l1!=null)
{
temp_val = l1.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l1 = l1.next;
go = temp_val /10;
}
while(l2!=null)
{
temp_val = l2.val + go;
ListNode temp2 = new ListNode(temp_val%10);
temp.next = temp2;
temp = temp2;
l2 = l2.next;
go = temp_val /10;
}
if(go != 0)
{
temp.next = new ListNode(go);
}
return result;
}
}
解法二:较优的解法,但在leetcode上运行时间要劣于前者
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;//表示进位
ListNode head = new ListNode(0);//首前节点,参照C++的尾后节点
ListNode temp = head;
//一直循环至将两个链表遍历完全才退出
while((l1 != null)||(l2 != null))
{
//仅需确定和的几个因子大小即可;
int first = (l1 != null) ? l1.val : 0;//使用if语句亦可
int second = (l2 != null) ? l2.val : 0;
int result = first + second + carry;//每一次循环计算结果
carry = result /10;
ListNode pNode = new ListNode(result % 10);
temp.next = pNode;
temp = pNode;//准备下一循环
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
//还需考虑carray不等于0及仍有进位的情况
if (carry > 0)
{
temp.next = new ListNode(carry);
}
return head.next;//注意此返回值
}
}
相关文章推荐
- leetcode-2 Add Two Numbers 计算两个对应的列表和问题
- 【LeetCode91-100】编码种数,逆转单链表,IP地址转化,中序遍历二叉树,生成二叉搜索树,计算二叉树个数,交叉string【hard】,判断二叉搜索树是否合法,恢复二叉树(有两个元素被交换)
- LeetCode 445. Add Two Numbers II--两个链表均按照由尾部到头部计算两个结点数值之和,保持进位
- 判断单链表是否存在环,判断两个链表是否相交问题详解(转载)
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 判断单链表是否存在环,判断两个链表是否相交问题详解(转载)
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 用单循环链表存储一个环上的数据,并计算任意两个相邻元素之差是否超过2
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- PHP中计算两个日期之差的问题
- 判断单链表是否存在环,判断两个链表是否相交问题详解(转载)
- 小算法系列-判断单链表是否存在环,判断两个链表是否相交问题详解(转)
- 判断单链表是否存在环,判断两个链表是否相交问题详解
- 判断单链表是否存在环,判断两个链表是否相交问题详解(转载)