Java反轉(zhuǎn)鏈表測試過程介紹
導(dǎo)讀
本文主體為單項鏈表和雙向鏈表的反轉(zhuǎn)以及簡單的測試,以便于理解鏈表相關(guān)的算法題目。
鏈表
特點
- 便于增刪數(shù)據(jù),不便于尋址
- 在內(nèi)存中屬于跳轉(zhuǎn)結(jié)構(gòu)
單鏈表和雙鏈表的定義
單鏈表: 值,一條next指針
雙鏈表:值,一條last指針,一條next指針
單向鏈表
Node結(jié)點
public static class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
ArrayList<Integer> nums = new ArrayList<>();
Node node = this;
while (node != null) {
nums.add(node.value);
node = node.next;
}
return nums.toString();
}
}反轉(zhuǎn)單向鏈表
public static Node reverseLinkedList(Node head) {
Node next = null;
Node pre = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
測試函數(shù)
public static void main(String[] args) {
Node node = new Node(1);
node.next = new Node(2);
node.next.next = new Node(3);
node = reverseLinkedList(node);
System.out.println(node);
}
輸出結(jié)果如下:
[3, 2, 1]
雙向鏈表
Node結(jié)點
public static class DoubleList {
public int value;
public DoubleList next;
public DoubleList last;
public DoubleList(int value) {
this.value = value;
}
@Override
public String toString() {
ArrayList<Integer> nums = new ArrayList<>();
DoubleList node = this;
while (node != null) {
nums.add(node.value);
node = node.next;
}
return nums.toString();
}
}反轉(zhuǎn)雙向鏈表
public static DoubleList reverseDoubleList(DoubleList head) {
DoubleList next;
DoubleList pre = null;
while (head != null) {
next = head.next;
head.next = pre;
// 注意和單項鏈表不一樣的地方只有這一行,其他都基本一樣
head.last = next;
pre = head;
head = next;
}
return pre;
}
測試代碼
public static void main(String[] args) {
DoubleList node1 = new DoubleList(1);
node1.next = new DoubleList(2);
node1.next.last = node1;
node1.next.next = new DoubleList(3);
node1.next.next.last = node1.next;
System.out.println("reverseDoubleList(node1) = " + reverseDoubleList(node1));
}
輸出結(jié)果如下:
reverseDoubleList(node1) = [3, 2, 1]
到此這篇關(guān)于Java反轉(zhuǎn)鏈表測試過程介紹的文章就介紹到這了,更多相關(guān)Java反轉(zhuǎn)鏈表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址
這篇文章主要介紹了如何利用Java語言實現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定的參考價值,快跟隨小編一起學(xué)習(xí)一下吧2022-06-06
Mybatis中強(qiáng)大的resultMap功能介紹
這篇文章主要給大家介紹了關(guān)于Mybatis中強(qiáng)大的resultMap功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
在springboot中對kafka進(jìn)行讀寫的示例代碼
本篇文章主要介紹了在springboot中對kafka進(jìn)行讀寫的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Java二維數(shù)組與稀疏數(shù)組相互轉(zhuǎn)換實現(xiàn)詳解
在某些應(yīng)用場景中需要大量的二維數(shù)組來進(jìn)行數(shù)據(jù)存儲,但是二維數(shù)組中卻有著大量的無用的位置占據(jù)著內(nèi)存空間,稀疏數(shù)組就是為了優(yōu)化二維數(shù)組,節(jié)省內(nèi)存空間2022-09-09
java實現(xiàn)cassandra高級操作之分頁實例(有項目具體需求)
這篇文章主要介紹了java實現(xiàn)cassandra高級操作之分頁實例(有項目具體需求),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
Mybatisplus創(chuàng)建Spring?Boot工程打包錯誤的解決方式
最近在實戰(zhàn)springboot遇到了一些坑,記錄一下,下面這篇文章主要給大家介紹了關(guān)于Mybatisplus創(chuàng)建Spring?Boot工程打包錯誤的解決方式,文中通過圖文介紹的介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

