Java實(shí)現(xiàn)單鏈表反轉(zhuǎn)的多種方法總結(jié)
對(duì)于單鏈表不熟悉的可以看一下基于Java實(shí)現(xiàn)單鏈表的增刪改查
一、原地反轉(zhuǎn)
1、新建一個(gè)哨兵節(jié)點(diǎn)下一結(jié)點(diǎn)指向頭結(jié)點(diǎn)
2、把待反轉(zhuǎn)鏈表的下一節(jié)點(diǎn)插入到哨兵節(jié)點(diǎn)的下一節(jié)點(diǎn)
反轉(zhuǎn)之前的鏈表:1–>2–>3–>4>–>5
加入哨兵節(jié)點(diǎn):dummp–>1–>2–>3–>4>–>5
原地反轉(zhuǎn):
定義:prev=dummp.next; pcur=prev.next;
prev.next=pcur.next;
pcur.next=dummp.next;
dummp.next=pcur;
pcur=prev.next;


public Stu_node reverse_list(Stu_node head){
if (head.next==null ||head.next.next==null)
return null;
Stu_node dump = new Stu_node(-1," ");
dump.next=head;
Stu_node prev = dump.next;
Stu_node pcur = prev.next;
while(pcur!=null){
prev.next=pcur.next;
pcur.next=dump.next;
dump.next=pcur;
pcur=prev.next;
}
return dump.next;
}
二、新建鏈表頭結(jié)點(diǎn)插法
二、新建鏈表頭結(jié)點(diǎn)插法:
新建一個(gè)頭結(jié)點(diǎn),遍歷原鏈表,把每個(gè)節(jié)點(diǎn)用頭結(jié)點(diǎn)插入到新建鏈表中。最后,新建的鏈表就是反轉(zhuǎn)后的鏈表。


public Stu_node reverse_list1 (Stu_node head){
//新建一個(gè)新的鏈表的頭結(jié)點(diǎn)
Stu_node dump = new Stu_node(-1," ");
Stu_node pcur = head;
//遍歷待反轉(zhuǎn)鏈表,頭結(jié)點(diǎn)插入到新的鏈表中
while(pcur!=null){
Stu_node pnext = pcur.next;
pcur.next = dump.next;
dump.next=pcur;
pcur=pnext;
}
//新鏈表頭結(jié)點(diǎn)不是需要返回的數(shù)據(jù),因此返回頭結(jié)點(diǎn)的下一節(jié)點(diǎn)
return dump.next;
}
三、利用棧結(jié)構(gòu)實(shí)現(xiàn)鏈表的反轉(zhuǎn)
由于棧結(jié)構(gòu)存儲(chǔ)數(shù)據(jù)是先進(jìn)后出(后進(jìn)先出)也可以通過(guò)棧達(dá)到反轉(zhuǎn)鏈表的目的。
public Stu_node reverse_stack(Stu_node head){
Stack<Stu_node> stack = new Stack<>();
Stu_node temp = head;
//鏈表入棧
while(temp!=null){
stack.push(temp);
temp=temp.next;
}
//取出棧中的一個(gè)節(jié)點(diǎn)當(dāng)做新的鏈表的頭結(jié)點(diǎn)
Stu_node new_head = stack.pop();
Stu_node cur = new_head;
//出站
while(!stack.isEmpty()){
Stu_node node = stack.pop();
//將出站的節(jié)點(diǎn)指向取消
node.next=null;
//將新的鏈表串起來(lái)
cur.next = node;
cur = node;
}
return new_head;
}
四、完整代碼奉上
import java.util.Stack;
public class revere_node {
public static void main(String[] args) {
LinkedNode list= new LinkedNode();
Stu_node node1 = new Stu_node(1,"張三");
Stu_node node2 = new Stu_node(2,"李四");
Stu_node node3 = new Stu_node(3,"王二");
Stu_node node4 = new Stu_node(4,"麻子");
Stu_node node5 = new Stu_node(5,"趙六");
//打印添加節(jié)點(diǎn)之前的鏈表
list.print();
//尾結(jié)點(diǎn)添加節(jié)點(diǎn)
list.add(node1);
list.add(node2);
list.add(node3);
list.add(node4);
list.add(node5);
//打印添加加點(diǎn)之后的鏈表
list.print();
System.out.println("-------------------");
//定義一個(gè)頭結(jié)點(diǎn)接收調(diào)用函數(shù)返回的頭節(jié)點(diǎn)
Stu_node head = list.reverse_stack(list.head);
//遍歷輸出每個(gè)節(jié)點(diǎn)
while (head.next!=null){
System.out.println(head);
head=head.next;
}
}
}
//定義一個(gè)鏈表的操作類(lèi)
class LinkedNode{
//定義一個(gè)頭結(jié)點(diǎn)
Stu_node head = new Stu_node(-1," ");
//添加鏈表的方法
public void add(Stu_node node){
Stu_node temp = head;
while(true){
if (temp.next==null)
break;
temp=temp.next;
}
temp.next=node;
}
//打印鏈表
public void print(){
Stu_node temp = head.next;
if (head.next==null){
System.out.println("此鏈表為空");
}
while (temp!=null){
System.out.println(temp);
temp=temp.next;
}
}
//原地反轉(zhuǎn)
public Stu_node reverse_list(Stu_node head){
if (head.next==null ||head.next.next==null)
return null;
Stu_node dump = new Stu_node(-1," ");
dump.next=head;
Stu_node prev = dump.next;
Stu_node pcur = prev.next;
while(pcur!=null){
prev.next=pcur.next;
pcur.next=dump.next;
dump.next=pcur;
pcur=prev.next;
}
return dump.next;
}
//新建一個(gè)新的鏈表,頭結(jié)點(diǎn)插入法實(shí)現(xiàn)鏈表的反轉(zhuǎn)
public Stu_node reverse_list1 (Stu_node head){
Stu_node dump = new Stu_node(-1," ");
Stu_node pcur = head;
while(pcur!=null){
Stu_node pnext = pcur.next;
pcur.next = dump.next;
dump.next=pcur;
pcur=pnext;
}
return dump.next;
}
//利用棧實(shí)現(xiàn)反轉(zhuǎn)鏈表
public Stu_node reverse_stack(Stu_node head){
Stack<Stu_node> stack = new Stack<>();
Stu_node temp = head;
//鏈表入棧
while(temp!=null){
stack.push(temp);
temp=temp.next;
}
//取出一個(gè)節(jié)點(diǎn)當(dāng)做新的鏈表的頭結(jié)點(diǎn)
Stu_node new_head = stack.pop();
Stu_node cur = new_head;
//出站
while(!stack.isEmpty()){
Stu_node node = stack.pop();
//將出站的節(jié)點(diǎn)指向取消
node.next=null;
//將新的鏈表串起來(lái)
cur.next = node;
cur = node;
}
return new_head;
}
}
//節(jié)點(diǎn)類(lèi)
class Stu_node{
int num;
String name;
Stu_node next;
//重寫(xiě)toString方法,顯示節(jié)點(diǎn)數(shù)據(jù)
@Override
public String toString() {
return "Stu_node{" +
"num=" + num +
", name='" + name + '\'' +
'}';
}
public Stu_node(int num, String name) {
this.num = num;
this.name = name;
}
}
總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn)單鏈表反轉(zhuǎn)的多種方法的文章就介紹到這了,更多相關(guān)Java單鏈表反轉(zhuǎn)方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Java多線程notify與notifyall的區(qū)別分析
本篇文章對(duì)Java中多線程notify與notifyall的區(qū)別進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05
將Springboot項(xiàng)目升級(jí)成Springcloud項(xiàng)目的圖文教程
本文主要介紹了將Springboot項(xiàng)目升級(jí)成Springcloud項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
IDEA?報(bào)Plugin'maven-resources-plugin:'not?found?
如果在使用?IDEA?時(shí)遇到?"Plugin?'maven-resources-plugin:'?not?found"?錯(cuò)誤,可能是由于?Maven?倉(cāng)庫(kù)中未找到所需的?Maven?插件,近小編給大家分享幾種解決方法,感興趣的朋友跟隨小編一起看看吧2023-07-07
Java中四種9*9乘法表的實(shí)現(xiàn)方式(附代碼)
這篇文章主要介紹了Java中四種9*9乘法表的實(shí)現(xiàn)方式(附代碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot使用自定義注解+AOP+Redis實(shí)現(xiàn)接口限流的實(shí)例代碼
這篇文章主要介紹了SpringBoot使用自定義注解+AOP+Redis實(shí)現(xiàn)接口限流,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
如何解決springboot自動(dòng)重啟問(wèn)題
這篇文章主要介紹了如何解決springboot自動(dòng)重啟問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
spring監(jiān)視器actuator配置應(yīng)用
這篇文章主要介紹了spring監(jiān)視器actuator配置應(yīng)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
自帶IDEA插件的阿里開(kāi)源診斷神器Arthas線上項(xiàng)目BUG調(diào)試
這篇文章主要為大家介紹了自帶IDEA插件阿里開(kāi)源診斷神器Arthas線上項(xiàng)目BUG調(diào)試,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法
這篇文章主要介紹了SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03

