递归与非递归法实现链表相加 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
譯文:
你有兩個(gè)由單鏈表表示的數(shù)。每個(gè)結(jié)點(diǎn)代表其中的一位數(shù)字。數(shù)字的存儲(chǔ)是逆序的, 也就是說(shuō)個(gè)位位于鏈表的表頭。寫一函數(shù)使這兩個(gè)數(shù)相加并返回結(jié)果,結(jié)果也由鏈表表示。
例子:(7-> 1 -> 6), (5 -> 9 -> 2)
輸入:2 -> 1 -> 9
進(jìn)階:
考慮鏈表是反向的,比如:
例子:(6-> 1 -> 7), (2 -> 9 -> 5)
輸入:9 -> 1 -> 2
考慮遞歸法與非遞歸法實(shí)現(xiàn)原題和進(jìn)階問(wèn)題(Follow Up)。
非遞歸方法:
我們必須保存每次結(jié)果的carryOn即進(jìn)位值,在第一種情況下,我們需要知道,表頭的數(shù)據(jù)是最低位,這意味著從表頭即可開(kāi)始加兩個(gè)數(shù),并且把carryOn (1,或0)合理的傳遞給下一次運(yùn)算。
首先建立鏈表類如下:
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]");
}
}
注意我同時(shí)建立了函數(shù)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();
}
遞歸方法實(shí)現(xiàn)FollowUp問(wèn)題
當(dāng)鏈表反轉(zhuǎn),即表尾變成了加運(yùn)算的最低位時(shí),上述方法變的相對(duì)麻煩很多,這時(shí)遞歸方法顯得更加方便。
核心代碼:
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;
}
}
函數(shù)addingTwoLinkedList的返回值是向其上層(previous Node)進(jìn)位的數(shù)值,非0即1. 函數(shù)的參數(shù)需要輸入兩個(gè)被加數(shù)的表頭即可。
遞
轉(zhuǎn)載于:https://blog.51cto.com/jamesd1987/1349625
總結(jié)
以上是生活随笔為你收集整理的递归与非递归法实现链表相加 CC150 V5 2.5题 java版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SRM 440(1-250pt, 1-5
- 下一篇: 【转】测试人员的思想理念和工作方法