java實(shí)現(xiàn)鏈表反轉(zhuǎn)
本文為大家分享了java實(shí)現(xiàn)鏈表反轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
算法題:實(shí)現(xiàn)鏈表的反轉(zhuǎn)
提供了2種方法,迭代法、遞歸法。
(為了方便輸出可視化,在自定義的ListNode中重寫(xiě)了toString方法。)
/**
* Created By --- on 2021/8/12
* 以下代碼可以直接粘貼進(jìn)編譯器輸出
*/
public class ReverseList {
public static void main(String[] args) {
ListNode head = new ListNode(3, new ListNode(5, new ListNode(8, new ListNode(9))));
System.out.println("初始鏈表:" + head);
ListNode newList = reverseList(head);
System.out.println("使用迭代法反轉(zhuǎn)鏈表:" + newList);
ListNode newList2 = reverseList2(null, newList);
System.out.println("使用遞歸法反轉(zhuǎn)鏈表:" + newList2);
}
/**
* 迭代法
*/
public static ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
ListNode tmp;
while (cur != null) {
tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
/**
* 遞歸法
*/
public static ListNode reverseList2(ListNode pre, ListNode cur) {
if (cur == null) {
return pre;
}
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
return reverseList2(pre, cur);
}
}
/**
* singly-linked list
*/
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(String.valueOf(val));
ListNode next = this.next;
while (next != null) {
sb.append(next.val);
next = next.next;
}
return sb.toString();
}
}
輸出結(jié)果:

再為大家分享一段java實(shí)現(xiàn)鏈表反轉(zhuǎn)的三種方式
分別通過(guò)棧、遞歸、指針的方式實(shí)現(xiàn):
import java.util.Stack;
public class ReverseLinkedList {
public static void main(String[] args) {
ReverseLinkedList reverseLinkedList = new ReverseLinkedList();
reverseLinkedList.test();
}
public void test() {
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
node1.setNext(node2);
node2.setNext(node3);
//方法需要替換
node1 = reverseByPointer(node1);
while (node1 != null) {
System.out.println(node1.val);
node1 = node1.getNext();
}
}
//棧實(shí)現(xiàn)
private Node reverseByStack(Node head) {
if (head == null || head.getNext() == null) {
return head;
}
Stack<Node> stack = new Stack<>();
while (head != null) {
stack.push(head);
head = head.getNext();
}
head = stack.pop();
Node tmp = head;
while (!stack.empty()) {
Node node = stack.pop();
node.setNext(null);
tmp.setNext(node);
tmp = node;
}
return head;
}
//遞歸實(shí)現(xiàn)
private Node reverseByRecursion(Node head) {
if (head == null || head.getNext() == null) {
return head;
}
//遞歸獲取當(dāng)前節(jié)點(diǎn)的后一個(gè)節(jié)點(diǎn)
Node tmp = reverseByRecursion(head.getNext());
Node node = head.getNext();
head.setNext(null);
node.setNext(head);
return tmp;
}
//指針實(shí)現(xiàn)
private Node reverseByPointer(Node head) {
if (head == null || head.getNext() == null) {
return head;
}
//pre指針指向前一個(gè)節(jié)點(diǎn),初始第一個(gè)節(jié)點(diǎn)的前節(jié)點(diǎn)為空
Node pre = null;
//tmp指針指向當(dāng)前節(jié)點(diǎn)
Node tmp = null;
while (head != null) {
//tmp指針指向head頭指針節(jié)點(diǎn)
tmp = head;
//head頭指針向后遍歷
head = head.getNext();
//反轉(zhuǎn),設(shè)置當(dāng)前節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)為前一個(gè)節(jié)點(diǎn)
tmp.setNext(pre);
//pre指針向后移動(dòng),指向當(dāng)前節(jié)點(diǎn)
pre = tmp;
}
return tmp;
}
private class Node {
private int val;
private Node next;
public Node(int val) {
this.val = val;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis中updateBatch實(shí)現(xiàn)批量更新
本文主要介紹了Mybatis中updateBatch實(shí)現(xiàn)批量更新,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Java并發(fā)應(yīng)用之任務(wù)執(zhí)行分析
這篇文章主要為大家詳細(xì)介紹了JavaJava并發(fā)應(yīng)用編程中任務(wù)執(zhí)行分析的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07
SpringBoot@DeleteMapping(/xxx/{id})請(qǐng)求報(bào)405的解決
這篇文章主要介紹了SpringBoot@DeleteMapping(/xxx/{id})請(qǐng)求報(bào)405的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Java操作Elasticsearch?rest-high-level-client?的基本使用
這篇文章主要介紹了Java操作Elasticsearch?rest-high-level-client?的基本使用,本篇主要講解一下?rest-high-level-client?去操作?Elasticsearch的方法,結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10
Java中Vector與ArrayList的區(qū)別詳解
本篇文章是對(duì)Java中Vector與ArrayList的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Java實(shí)現(xiàn)簡(jiǎn)易提款機(jī)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易提款機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
為何修改equals方法時(shí)還要重寫(xiě)hashcode方法的原因分析
這篇文章主要介紹了為何修改equals方法時(shí)還要重寫(xiě)hashcode方法的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

