java單鏈表逆序用法代碼示例
本篇博客,比較簡(jiǎn)單。對(duì)單鏈表逆序不理解的看看就可以了。
逆序思想
現(xiàn)假設(shè)有一鏈表,有待逆序操作。我們首先想到的就是將那個(gè)指針關(guān)系逆序了就行了唄。
事實(shí)上,就是這樣。博主就是以這個(gè)為目標(biāo)來(lái)完成的單鏈表逆序操作。
Node pre = null;
Node post = null;
while(head!=null){
post = head.next;
head.next = pre;
pre = head;
head = post;
}
這便是逆序的核心了。下面我們就來(lái)一步步的講解。
- 首次逆序:
一開(kāi)始的話(huà),pre,post都設(shè)置為null。這是必須的,因?yàn)樵趆ead.next=pre這行代碼執(zhí)行完成后,我們?cè)嫉哪莻€(gè)head節(jié)點(diǎn)的next將變成null,也就是我們整個(gè)鏈表的null了。
想象一下,原來(lái)的那個(gè)鏈表的最后面的next不也是一個(gè)null嗎?這里道理是一致的。
此時(shí),更新pre為原來(lái)的head節(jié)點(diǎn),也是為了下一步的逆序做準(zhǔn)備,而head也自然的變成了原來(lái)的head.next了。
- 不斷逆序。

抱歉,手抖了一下,畫(huà)錯(cuò)了。大家見(jiàn)諒。手繪圖上的第五次示意pre節(jié)點(diǎn)應(yīng)該在節(jié)點(diǎn)5的位置,沒(méi)有了head。
從圖例中我們也不難看出,我們就是一次次的將head向后移,同時(shí)更新pre節(jié)點(diǎn),來(lái)達(dá)到逆序的效果。
代碼
package list;
public class ReverseList {
public static void main(String[] args) {
Node head = new Node(1);
int[] value = {2,3,4,5};
Node temp = head;
for (int i = 0 ; i< value.length;i++) {
Node node = new Node(value[i]);
temp.next = node;
temp = temp.next;
}
printList(head);
// 反序輸出一個(gè)單鏈表
head = reverse(head);
printList(head);
// 再次反向
head = reverseSingleList(head);
printList(head);
}
public static void printList(Node head) {
while(head!=null) {
System.out.print("\t"+head.value);
head = head.next;
}
System.out.println();
}
public static Node reverse(Node head) {
Node pre = null;
Node post = null;
while(head!=null) {
post = head.next;
head.next = pre;
pre = head;
head = post;
}
return pre;
}
public static Node reverseSingleList(Node head) {
Node pre = null;
Node next = null;
while(head!=null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}
class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
測(cè)試
經(jīng)測(cè)試,代碼輸出正確。
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
幫助理解,下面是另一個(gè)實(shí)例:
/**
* java 實(shí)現(xiàn)單鏈表的逆序
* @author Administrator
*
*/
public class SingleLinkedReverse {
class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}
public static void main(String[] args) {
SingleLinkedReverse slr = new SingleLinkedReverse();
Node head, tail;
head = tail = slr.new Node(0);
for (int i=1; i<10; i++){
Node p = slr.new Node(i);
tail.next = p;
tail = p;
}
tail = head;
while(tail != null){
System.out.print(tail.data+" ");
tail = tail.next;
}
head = reverse(head);
System.out.println(" ");
while(head != null){
System.out.print(head.data+" ");
head = head.next;
}
}
private static Node reverse(Node head) {
Node p1,p2 = null;
p1 = head;
while(head.next != null){
p2 = head.next;
head.next = p2.next;
p2.next = p1;
p1 = p2;
}
return p2;
}
}
測(cè)試結(jié)果:
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
總結(jié)
以上就是本文關(guān)于java單鏈表逆序用法代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Spring框架學(xué)習(xí)之Spring?@Autowired實(shí)現(xiàn)自動(dòng)裝配的代碼
自動(dòng)裝配就是說(shuō),你不用手動(dòng)實(shí)現(xiàn)bean之間的組合關(guān)系,只要使用了@Autowired注解,程序就會(huì)自動(dòng)的注入這個(gè)需要的bean,前提是你的Spring容器有這個(gè)bean,這篇文章主要介紹了Spring?@Autowired實(shí)現(xiàn)自動(dòng)裝配,需要的朋友可以參考下2021-12-12
詳解Java回調(diào)的原理與實(shí)現(xiàn)
回調(diào)函數(shù),顧名思義,用于回調(diào)的函數(shù)?;卣{(diào)函數(shù)只是一個(gè)功能片段,由用戶(hù)按照回調(diào)函數(shù)調(diào)用約定來(lái)實(shí)現(xiàn)的一個(gè)函數(shù)。回調(diào)函數(shù)是一個(gè)工作流的一部分,由工作流來(lái)決定函數(shù)的調(diào)用(回調(diào))時(shí)機(jī)。2017-03-03
springboot+feign+Hystrix整合(親測(cè)有效)
本文主要介紹了springboot+feign+Hystrix整合,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況
這篇文章主要介紹了解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
java如何讀取resources目錄和同級(jí)目錄文件
介紹了Java中讀取resources目錄和同級(jí)目錄文件的方法,并討論了在IDE和發(fā)布環(huán)境中可能遇到的問(wèn)題,通過(guò)測(cè)試發(fā)現(xiàn),執(zhí)行目錄可能會(huì)影響文件讀取,建議在使用`user.dir`時(shí)注意jar包的運(yùn)行目錄問(wèn)題2024-12-12
解決StringBuffer和StringBuilder的擴(kuò)容問(wèn)題
這篇文章主要介紹了解決StringBuffer和StringBuilder的擴(kuò)容問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
關(guān)于java命令的本質(zhì)邏輯揭秘過(guò)程
Java是通過(guò)java虛擬機(jī)來(lái)裝載和執(zhí)行編譯文件(class文件)的,java虛擬機(jī)通過(guò)命令java option 來(lái)啟動(dòng),這篇文章主要給大家介紹了關(guān)于java命令的本質(zhì)邏輯揭秘的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-05-05

