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 的代码

最新文章

  1. 【leetcode】Restore IP Addresses
  2. ESL python调用C模块时传递unicode字符串报错问题解决
  3. asp.net下载文件方法
  4. Idea_Maven配置
  5. 【翻译】Kinect v2程序设计(C++) BodyIndex篇
  6. 黄聪:wordpress更新失败‘C:\Windows\TEMP/wordpress.tmp’,更换临时保存路径的解决办法
  7. TGL站长关于常见问题的回复
  8. html5.js
  9. cocos2dx 3.1从零学习(三)——Touch事件(回调,反向传值)
  10. 《JS权威指南学习总结--第五章语句》
  11. JavaScript获取浏览器信息的方法
  12. angular2使用官网npm install下载依赖失败的处理方法
  13. [转帖]Kerberos和NTLM - SQL Server
  14. 每10秒执行定时任务-crontab
  15. 九、将cs文件快速的转换成可执行文件和响应文件(配置编译开关的文件)
  16. ZOJ 3203 Light Bulb
  17. 算法导论-MIT笔记
  18. 20155220吴思其 实验2 Windows口令破解
  19. Linux命令应用大词典-第39章 网络安全
  20. PHP 统计数据功能 有感

热门文章

  1. 神经网络可以拟合任意函数的视觉证明A visual proof that neural nets can compute any function
  2. python的单元测试框架
  3. 如何在SharePoint的列表中使用通配符来filter出ListItem?
  4. AJAX前台传过来的中文在后台获取是乱码问题
  5. [Python爬虫] 之八:Selenium +phantomjs抓取微博数据
  6. hdu 1158-Employment Planning,n*n*n
  7. Report Studio中树提示如何使用
  8. Yii学习笔记之二(使用gii生成一个简单的样例)
  9. [Angular-Scaled Web] 6. Navigating between states with ui-router
  10. (剑指Offer)面试题42:左旋转字符串