The two numbers and return it as a linked list
来源:互联网 发布:3d模型制作软件 编辑:程序博客网 时间:2024/05/20 09:45
/**
*
* 原题
* 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
*
* 题目大意
* 有两个单链表,代表两个非负数,每一个节点代表一个数位,数字是反向存储的,
* 即第一个结点表示最低位,最后一个结点表示最高位。求两个数的相加和,并且以链表形式返回。
* 解题思路
* 由于相加时都是对应的位相加,所以我采用递归的方法进行计算;先从个位算起,如果大于10,则取出余数,留在本位,如果大于10,则取整,給下十位位相加,进行计算,一次类推
*
*
* @param l1 第一个数
* @param l2 第二个数
* @return 结果
* @date 2017年2月4日 上午9:31:47
*/
public class LinkedNode { int value; LinkedNode next; public LinkedNode(int value) { this.value = value; }
public class Solution { public LinkedNode handler(LinkedNode l1,LinkedNode l2){ return helper(l1,l2,0); } public LinkedNode helper(LinkedNode l1,LinkedNode l2,int carry){ if (l1 == null && l2 == null) { return carry == 0? null : new LinkedNode(carry); } if (l1 == null && l2 != null) { l1 = new LinkedNode(0); } if (l1 != null && l2== null) { l2 = new LinkedNode(0); } LinkedNode result = new LinkedNode(-1); int sum = l1.value + l2.value + carry; result.value = sum % 10;//取余用于本位使用 carry = sum / 10;//取整 用于进阶使用 result.next = helper(l1.next, l2.next, carry);// int sum = l1.val + l2.val + carry;// ListNode curr = new ListNode(sum % 10);//取余// curr.next = helper(l1.next, l2.next, sum / 10); return result; } public void outputResult(LinkedNode result){ if (result != null) { System.out.print("-->"+result.value); outputResult(result.next); } }
0 0
- The two numbers and return it as a linked list
- [CrackCode] 2.4 Add two numbers and return the sum as a linked list
- 【12】Adds two numbers represented by a linked list and returns the sum as a linked list
- Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
- Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers
- add two linked list as integer
- add two linked list as integer
- 2. Add Two Numbers(Linked List)
- Ch2-2: return the nth to the last node data of a singly linked list
- Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar
- define a class for a linked list and write a method to delete the nth node.
- Ch2-5: find the beginning of loop in a circular linked list---two solutions
- Linked List -- Leetcode problem 2. Add Two Numbers
- LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, returnn
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- 【Linked-list专题-1】445. Add Two Numbers II 328. Odd Even Linked List
- CF850B-Arpa and a list of numbers
- 在Android中用e.printStackTrace()
- Spring Boot日志管理
- POJ - 1125 Stockbroker Grapevine解题报告
- linux命令英文缩写的含义(方便记忆)
- 机器学习问题解决思路
- The two numbers and return it as a linked list
- 谷歌键盘切换26键与九宫格中文输入法
- 微信小程序----关于变量对象data 和 前端wxml取后台js变量值
- 指定的域不存在,或无法联系
- 缓存失效,导致高并发穿透DB的几种解决思路
- Android Studio 使用小技巧和快捷键
- LeetCode之191 Number of 1 Bits 别名:数1
- 编程模拟自然(九):元胞自动机
- 基于CENTOS的linux指令基础学习