LeetCode Add Two Numbers
来源:互联网 发布:java lambda filter 编辑:程序博客网 时间:2024/05/19 17:05
Description:
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Solution:
First we have to be aware that each number is listed in a list, and what we need to do is add two numbers in the form of list.
And we can use loops to add two numbers.
In the first loop, we use a loop to add common digits of two numbers by adding the corresponding digits and also digit 0/1 for the next digit.
Then we use a single loop to calculate the remaining part of number 1 or number 2.
Finally, we prune all the 0s in the tail.
/** * Definition for singly-linked list. public class ListNode { int val; ListNode * next; ListNode(int x) { val = x; } } */public class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode l = new ListNode(0);ListNode tot1 = l1, tot2 = l2, tot = l;int temp;ListNode next;while (tot1 != null && tot2 != null) {temp = tot.val + tot1.val + tot2.val;if (temp < 10) {tot.val = temp;tot.next = new ListNode(0);} else {tot.val = temp - 10;tot.next = new ListNode(1);}tot1 = tot1.next;tot2 = tot2.next;if ((tot1 == null && tot2 == null) && temp < 10) {tot.next = null;break;}tot = tot.next;}while (tot1 != null) {temp = tot.val + tot1.val;//System.out.println(tot1.val + " " + +temp);if (temp < 10) {tot.val = temp;tot.next = tot1.next;break;}tot.val = temp - 10;tot.next = new ListNode(1);tot1 = tot1.next;tot = tot.next;}//print(l);while (tot2 != null) {temp = tot.val + tot2.val;if (temp < 10) {tot.val = temp;tot.next = tot2.next;break;}tot.val = temp - 10;tot.next = new ListNode(1);tot2 = tot2.next;tot = tot.next;}//print(l);if (tot.val == 0) {tot = null;}//print(l);return l;}void print(ListNode l) {ListNode temp = l;while (temp != null) {System.out.print(temp.val + " ");temp = temp.next;}System.out.println();}public static void main(String[] args) {ListNode node1 = new ListNode(9);ListNode node2 = new ListNode(1);ListNode node3 = new ListNode(9);ListNode node4 = new ListNode(1);node1.next = node3;// node2.next = node4;Solution s = new Solution();s.addTwoNumbers(node1, node2);}}
- LeetCode: Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- Leetcode : Add Two Numbers
- 【leetcode】Add Two Numbers
- [LeetCode]Add Two Numbers
- leetcode Add Two Numbers
- LeetCode-Add Two Numbers
- [leetcode] Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode - Add Two Numbers
- LeetCode | Add Two Numbers
- 【leetcode】Add Two Numbers
- LinearLayout和RelativeLayout 比较
- Jfinal文件的导入导出
- 第八周项目三——分数类中的运算符重载(2)
- 教师兼干部类
- 第12周项目 程序阅读(5.c)
- LeetCode Add Two Numbers
- 第十周项目 0 阅读程序(1)
- 会员卡管理系统技术解析(三)包的导入
- 转发与重定向
- 如果你喜欢上了一个程序员小伙
- 使用Socket进行大文件断点上传续传
- 【iFD出品】iOS开发者Xcode常见错误整理(持续更新中)
- 第12周项目1 - 教师兼干部类
- MySQL导入.sql文件及常用命令