日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

递归与非递归法实现链表相加 CC150 V5 2.5题 java版

發(fā)布時(shí)間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 递归与非递归法实现链表相加 CC150 V5 2.5题 java版 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言:這是一道很有意思的題目,原題如下:


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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。