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.

 
Example

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】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 167. Add Two Numbers【LintCode by java】

    Description You have two numbers represented by a linked list, where each node contains a single dig ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

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

  8. 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 ...

  9. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

随机推荐

  1. android RecyclerView (二) ItemDecoration 详解

    RecyclerView 已经推出了一年多了,日常开发中也已经彻底从 ListView 迁移到了 RecyclerView,但前两天有人在一个安卓群里面问了个关于最顶上的 item view 加蒙层的 ...

  2. 数学图形(2.2)N叶结

    上一节讲的三叶结,举一反三,由三可到无穷,这一节讲N叶结 再次看下三叶结的公式: x = sin(t) + 2*sin(2*t)y = cos(t) - 2*cos(2*t) 将其改为: x = si ...

  3. SQL CREATE INDEX

    n relational database, Index will be the important mechanism for boosting performance. Index is impo ...

  4. Hadoop中Partition深度解析

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-partitioner.html,转载请注明源地址. 旧版 API 的 Partitioner 解析 P ...

  5. MSSQL 触发器 暂停 和 启动

    开启关闭触发器 禁用: ALTER TABLE member DISABLE TRIGGER trig1 GO 恢复: ALTER TABLE member ENABLE TRIGGER trig1 ...

  6. MYSQL 命令行工具自动登录的方法

    MYSQL 命令行工具自动登录的方法 1. 需求提出 由于在linux 环境下,经常需要使用mysql(command-line tool) 终端连接到MYSQL DB服务. 其中大致的语法如下: m ...

  7. 如何获取浏览器URL中查询字符串的参数?

    如何获取浏览器URL中查询字符串的参数? 想要知道怎样解决这个问题,首先我们先认识一下Location对象. Location对象包含了当前页面与位置(url)相关的信息 URL示例:http://w ...

  8. vsftpd 本地用户无法登陆 530 Login incorrect

    查看日志,监测用户无法登陆的错误日志 tail -f /var/log/secure 查看vsFTPd配置 /etc/vsftpd/vsftpd.conf 通过查看日志,发现用户的密码已经过期了... ...

  9. 教大家如何在word 2007中同时打出对齐上下标以及字母头上有波浪线(非编辑器)

    教大家如何在word 2007中打出(非编辑器): 如果要在多个字符串上面加上划线,可以使用一下步骤 按下“Ctrl+F9”组合键,出现“{}”,在{}中输入“EQ \x\to(要加上划线的字符串)” ...

  10. ARC和非ARC在项目中转换

    f your project doesn't use ARC: you must add the -fobjc-arc compiler flag to SVHTTPRequest.m andSVHT ...