您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何實現(xiàn)單鏈表遞歸反轉(zhuǎn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“如何實現(xiàn)單鏈表遞歸反轉(zhuǎn)”吧!
public class Node {private String name = null; private Node next = null; public Node(String name,Node next){this.name = name; this.next=next; }//遞歸反轉(zhuǎn)節(jié)點 public Node reverse(Node head){if(head==null || head.next ==null){return head; } Node temp = head.next; Node newHead = reverse(head.next); temp.next=head; head.next=null; return newHead; }//遍歷所有節(jié)點的名字 public String getNodeAllName(){ String names = ""; Node currNode = this; while (currNode!=null){ names += currNode.name+"->"; currNode = currNode.next; }return names; }public static void main(String[] args) { Node nodeC = new Node("C",null); Node nodeB = new Node("B",nodeC); Node nodeA = new Node("A",nodeB); System.out.println(nodeA.getNodeAllName()); //反轉(zhuǎn)A節(jié)點 Node reversedNode = nodeA.reverse(nodeA); System.out.println(reversedNode.getNodeAllName()); } }
到此,相信大家對“如何實現(xiàn)單鏈表遞歸反轉(zhuǎn)”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。