递归与非递归法实现链表相加 CC150 V5 2.5题 java版
前言:这是一道很有意思的题目,原题如下:
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
Input: (7 -> 1 -> 6), (5 -> 9 -> 2). That is to say you have to calculate 617+295 and return output:
Output: 2 -> 1 -> 9
Follow UP:
Suppose the digits are store in forward order, repeat the above problems
Input: (6 -> 1 -> 7), (2 -> 9 -> 5). That is to say you have to calculate 617+295 and return output:
Output: 9 -> 1 -> 2
译文:
你有两个由单链表表示的数。每个结点代表其中的一位数字。数字的存储是逆序的, 也就是说个位位于链表的表头。写一函数使这两个数相加并返回结果,结果也由链表表示。
例子:(7-> 1 -> 6), (5 -> 9 -> 2)
输入:2 -> 1 -> 9
进阶:
考虑链表是反向的,比如:
例子:(6-> 1 -> 7), (2 -> 9 -> 5)
输入:9 -> 1 -> 2
考虑递归法与非递归法实现原题和进阶问题(Follow Up)。
非递归方法:
我们必须保存每次结果的carryOn即进位值,在第一种情况下,我们需要知道,表头的数据是最低位,这意味着从表头即可开始加两个数,并且把carryOn (1,或0)合理的传递给下一次运算。
首先建立链表类如下:
private static class LinkedList{
private Node head;
public LinkedList (){
this.head = null;
}
public void insertNode (int value){
Node newNode = new Node(value);
newNode.prev = null;
newNode.next = this.head;
if (this.head!=null) this.head.prev = newNode;
this.head = newNode;
}
public void deleteNode(Node tobeDel){
//System.out.println("deleting:value["+tobeDel.value+"]"+tobeDel.next);
if (tobeDel == this.head) {
head = tobeDel.next;
head.prev = null;
}
if (tobeDel.prev!=null) tobeDel.prev.next = tobeDel.next;
if (tobeDel.next!=null) tobeDel.next.prev = tobeDel.prev;
}
public void printAllNodes(){
Node newNode = this.head;
while (newNode!=null){
if (newNode == head)
System.out.print("[Head]"+newNode.value);
else
System.out.print("->"+newNode.value);
newNode = newNode.next;
}
System.out.println("[End]");
}
}
注意我同时建立了函数printAllNodes为了方便的打印所有链表。
下面是Node的类:
private static class Node{
private int value;
private Node next;
private Node prev;
public Node (int value){
this.value = value;
this.prev = null;
this.next = null;
}
}
核心代码:
private static void CC2_5_1() {
// TODO Auto-generated method stub
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
LinkedList newList = new LinkedList();
list1.insertNode(6);
list1.insertNode(1);
list1.insertNode(7);
list2.insertNode(2);
list2.insertNode(9);
list2.insertNode(5);
list1.printAllNodes();
list2.printAllNodes();
Node point1 = list1.head;
Node point2 = list2.head;
int value=0,carryOn=0;
while (point1 !=null && point2 !=null){
value = point1.value + point2.value + carryOn;
carryOn = 0;
if (value >= 10){
value = value%10;
carryOn = 1;
}
newList.insertNode(value);
point1 = point1.next;
point2 = point2.next;
}
point1 = newList.head;
while (point1!=null){
newList.insertNode(point1.value);
newList.deleteNode(point1);
point1 = point1.next;
}
newList.printAllNodes();
}
递归方法实现FollowUp问题
当链表反转,即表尾变成了加运算的最低位时,上述方法变的相对麻烦很多,这时递归方法显得更加方便。
核心代码:
private static int addingTwoLinkedList(LinkedList newList,Node n1, Node n2){
int value = n1.value + n2.value;
if (n1.next ==null && n2.next == null){
newList.insertNode(value%10);
return value/10;
}
else{
value = value + addingTwoLinkedList(newList,n1.next,n2.next);
newList.insertNode(value%10);
return value/10;
}
}
函数addingTwoLinkedList的返回值是向其上层(previous Node)进位的数值,非0即1. 函数的参数需要输入两个被加数的表头即可。
递
转载于:https://blog.51cto.com/jamesd1987/1349625
递归与非递归法实现链表相加 CC150 V5 2.5题 java版相关推荐
- 逆置单链表——递归与非递归
文章目录 前言 方式一:非递归 原理 图解 实现代码 方式二:递归 原理 图解 实现代码 完整实现 代码 运行结果 前言 单链表的逆置图解 方式一:非递归 原理 非递归逆置单链表的本质是创建一个新的链 ...
- 单链表反转(递归和非递归)
单链表反转有递归和非递归两种算法. 下面定义节点 [cpp] view plaincopy typedef struct ListNode{ int value; ListNode* next; }L ...
- 关于回溯法的递归与非递归-----N皇后问题
关于回溯法的递归与非递归-–N皇后问题 蓝桥杯 基础练习 2n皇后问题 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一 ...
- 实验五 二叉树的递归及非递归的遍历及其应用
实验目的 熟练掌握二叉树的二叉链表存储结构的C语言实现.掌握二叉树的基本操作-前序.中序.后序遍历二叉树的三种方法.了解非递归遍历过程中"栈"的作用和状态,而且能灵活运用遍历算法实 ...
- python创建树结构、求深度_数据结构-树以及深度、广度优先遍历(递归和非递归,python实现)...
前面我们介绍了队列.堆栈.链表,你亲自动手实践了吗?今天我们来到了树的部分,树在数据结构中是非常重要的一部分,树的应用有很多很多,树的种类也有很多很多,今天我们就先来创建一个普通的树.其他各种各样的树 ...
- 二叉树的遍历(递归,非递归,Morris)
二叉树的遍历 目录 递归遍历 非递归遍历 Morris遍历 1. 递归遍历 递归版遍历只要当前节点不为null,就可以三次回到当前节点. public static void preOrderRecu ...
- 二叉树的遍历:先序 中序 后序遍历的递归与非递归实现及层序遍历
二叉树的定义:一种基本的数据结构,是一种每个节点的儿子数目都不多于2的树 树节点的定义如下: // 树(节点)定义 struct TreeNode {int data; // 值TreeNode* l ...
- 全排列递归与非递归python实现
全排列就是,给定一个序列,列举出该序列中元素所有的排列情况,列举方法有递归和非递归两种,详细可以见这位大神写的博客:https://blog.csdn.net/jopus/article/detail ...
- 二叉树,二叉树的归先序遍历,中序遍历,后序遍历,递归和非递归实现
二叉树,二叉树的归先序遍历,中序遍历,后序遍历,递归和非递归实现 提示:今天开始,系列二叉树的重磅基础知识和大厂高频面试题就要出炉了,咱们慢慢捋清楚! 文章目录 二叉树,二叉树的归先序遍历,中序遍历, ...
最新文章
- 【AllJoyn专题】基于AllJoyn和Yeelink的传感器数据上传与指令下行的研究
- 如何使用Openssl 制作CA证书
- python下载大文件-使用python通过FTP下载大文件
- [SQL] 外卖系统数据库设计
- linux看门狗树莓派,给树莓派安装看门狗
- Qt字符串转日期时间-毫秒
- 张文宏:WHO新型冠状病毒的传言和事实
- JS:键盘事件(onkeydown 、onkeypress、 onkeyup三个)
- 电磁干扰类型以及--电感和磁珠
- 网络—— 数据链路层,MTU
- 王垠:怎样尊重一个程序员
- 2022年高教杯国赛数学建模思路分享
- php手册下载打开没有内容,word保存了但是再打开就没有了怎么办
- 关系型数据库和非关系型数据库简单介绍:
- 技术人如何打造个人品牌,提高身价?
- Python 字典用法详解(超全)
- exe和dll放到不同目录中的方法
- 原生js去除前后空格
- 电荷泵的工作原理及常用电路
- H3C路由器PPPoE拨号 IPv6地址一直显示[TENTATIVE]的故障