java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼
java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解
實(shí)例代碼:
class Node { Node next; String name; public Node(String name) { this.name = name; } /** * 打印結(jié)點(diǎn) */ public void show() { Node temp = this; do { System.out.print(temp + "->"); temp = temp.next; }while(temp != null); System.out.println(); } /** * 遞歸實(shí)現(xiàn)單鏈表反轉(zhuǎn),注意:?jiǎn)捂湵磉^(guò)長(zhǎng),會(huì)出現(xiàn)StackOverflowError * @param n * @return */ public static Node recursionReverse(Node n) { long start = System.currentTimeMillis(); if(n == null || n.next == null) { return n; } Node reverseNode = recursionReverse(n.next); n.next.next = n; n.next = null; System.out.println("遞歸逆置耗時(shí):" + (System.currentTimeMillis() - start) + "ms..."); return reverseNode; } /** * 循環(huán)實(shí)現(xiàn)單鏈表反轉(zhuǎn) * @param n * @return */ public static Node loopReverse(Node n) { long start = System.currentTimeMillis(); if(n == null || n.next == null) { return n; } Node pre = n; Node cur = n.next; Node next = null; while(cur != null) { next = cur.next; cur.next = pre; pre = cur; cur = next; } n.next = null; n = pre; System.out.println("循環(huán)逆置耗時(shí):" + (System.currentTimeMillis() - start) + "ms..."); return pre; } @Override public String toString() { return name; } public static void main(String[] args) { int len = 10; Node[] nodes = new Node[len]; for(int i = 0; i < len; i++) { nodes[i] = new Node(i + ""); } for(int i = 0; i < len - 1; i++) { nodes[i].next = nodes[i+1]; } /* try { Thread.sleep(120000); } catch (InterruptedException e) { e.printStackTrace(); }*/ Node r1 = Node.loopReverse(nodes[0]); r1.show(); Node r = Node.recursionReverse(r1); r.show(); } }
總結(jié)
對(duì)于遞歸和循環(huán),推薦使用循環(huán)實(shí)現(xiàn),遞歸在單鏈表過(guò)大時(shí),會(huì)出現(xiàn)StatckOverflowError,遞歸涉及到方法的調(diào)用,在性能上也弱于循環(huán)的實(shí)現(xiàn)
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
MyBatis 中使用 Mapper 簡(jiǎn)化代碼的方法
這篇文章主要介紹了MyBatis 中使用 Mapper 簡(jiǎn)化代碼的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Springboot文件上傳出現(xiàn)找不到指定系統(tǒng)路徑的解決
這篇文章主要介紹了Springboot文件上傳出現(xiàn)找不到指定系統(tǒng)路徑的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08JAVA錯(cuò)誤類(lèi)結(jié)果類(lèi)和分頁(yè)結(jié)果類(lèi)代碼詳解
這篇文章主要介紹了JAVA錯(cuò)誤類(lèi)結(jié)果類(lèi)和分頁(yè)結(jié)果類(lèi)代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02springBoot解決static和@Component遇到的bug
這篇文章主要介紹了springBoot解決static和@Component遇到的bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java實(shí)現(xiàn)的微信圖片處理工具類(lèi)【裁剪,合并,等比例縮放等】
這篇文章主要介紹了Java實(shí)現(xiàn)的微信圖片處理工具類(lèi),可實(shí)現(xiàn)針對(duì)圖片的裁剪、合并、等比例縮放、旋轉(zhuǎn)、識(shí)別等各種常見(jiàn)的圖片處理功能,需要的朋友可以參考下2017-11-11JAVA正則表達(dá)式過(guò)濾文件的實(shí)現(xiàn)方法
這篇文章主要介紹了JAVA正則表達(dá)式過(guò)濾文件的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文大家能夠掌握理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12