package leadcode;

/**
* 2. Add Two Numbers
* Medium
* 4067
* 953
*
*
* You are given two non-empty linked lists representing two non-negative integers.
* 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.
*
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
*
* Example:
*
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
* Explanation: 342 + 465 = 807.
*/ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* } class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { }
}
*/
public class L2 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode cur = head;
int carry = 0;
while (l1!=null || l2!=null){
int x = (l1!=null)?l1.val:0;
int y = (l2!=null)?l2.val:0;
int sum = x+y+carry;
carry = sum/10;
cur.next = new ListNode(sum%10);
cur = cur.next;
if(l1!=null){
l1=l1.next;
}
if(l2!=null){
l2=l2.next;
}
}
if(carry!=0){
cur.next=new ListNode(carry);
}
return head.next;
} public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
} String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
} public static ListNode stringToListNode(String input) {
// Generate array from the input
int[] nodeValues = stringToIntegerArray(input); // Now convert that list into linked list
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for(int item : nodeValues) {
ptr.next = new ListNode(item);
ptr = ptr.next;
}
return dummyRoot.next;
} public static String listNodeToString(ListNode node) {
if (node == null) {
return "[]";
} String result = "";
while (node != null) {
result += Integer.toString(node.val) + ", ";
node = node.next;
}
return "[" + result.substring(0, result.length() - 2) + "]";
} public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
ListNode l1 = stringToListNode(line);
line = in.readLine();
ListNode l2 = stringToListNode(line); ListNode ret = new L2().addTwoNumbers(l1, l2); String out = listNodeToString(ret); System.out.print(out);
}
}
} class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}

[CareerCup] 2.5 Add Two Numbers 两个数字相加

Suppose the digits are stored in forward order. Repeat the above problem.
EXAMPLE
Input: (6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295.
Output: 9 -> 1 -> 2.That is, 912.

最新文章

  1. ECharts学习(2)--饼状图之南丁格尔图
  2. SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.4
  3. LR11启动卡修改
  4. 每日学习心得:$.extend()方法和(function($){...})(jQuery)详解
  5. SVN版本管理trunk及branch相关merge操作
  6. java开发规范总结_代码注释规范
  7. list-style-type -- 定义列表样式
  8. Php Laravel框架 多表关系处理 之 Eloquent一对多关系处理
  9. boost之ThreadPool
  10. javascript学习笔记(2)
  11. Eclipse乱码怎么办
  12. linux_ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机
  13. Collector
  14. 子div块中设置margin-top时影响父div块位置的解决办法
  15. 使用HAL库函数建立STM32F2工程
  16. Nginx反向代理和负载均衡的配置
  17. 前端UI框架《Angulr》入门
  18. ArcGIS 网络分析[8.1] 资料1 使用AO打开或创建网络数据集之【打开】
  19. Sublime中文编码问题
  20. 20165303魏煜第一周kali安装

热门文章

  1. GPIO输出—使用固件库点亮LED
  2. 拿与不拿的dfs
  3. CodeForces 459D Pashmak and Parmida&#39;s problem
  4. Apache Avro:一个新的数据交换格式
  5. spring中事务传播解读:PROPAGATION_REQUIRES_NEW
  6. exec系列函数(execl,execlp,execle,execv,execvp)使用
  7. springmvc+shiro认证框架配置
  8. C++ 类的对象管理模型初讲
  9. Tensorflow 梯度下降实例
  10. Unity基于DFGUI的TreeView设计