java實(shí)現(xiàn)單鏈表之逆序
下面一段代碼準(zhǔn)確的介紹了java實(shí)現(xiàn)單鏈表逆序,具體內(nèi)容就不做詳解了,有需要的朋友可以直接拷貝了
package com.ckw.mianshi;
/**
* 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
以上是java實(shí)現(xiàn)單鏈表逆序的代碼,希望大家能夠喜歡。相關(guān)文章
Mybatis的一級(jí)緩存和二級(jí)緩存原理分析與使用
mybatis-plus 是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,這篇文章帶你了解Mybatis的一級(jí)和二級(jí)緩存2021-11-11
Java 中的CharArrayReader 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
CharArrayReader 是字符數(shù)組輸入流。它和ByteArrayInputStream類(lèi)似,只不過(guò)ByteArrayInputStream是字節(jié)數(shù)組輸入流,而CharArray是字符數(shù)組輸入流。CharArrayReader 是用于讀取字符數(shù)組,它繼承于Reader2017-05-05
idea 修改項(xiàng)目名和module名稱(chēng)的操作
這篇文章主要介紹了idea 修改項(xiàng)目名和module名稱(chēng)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Spring?@Bean注解深入分析源碼執(zhí)行過(guò)程
隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用2023-01-01
關(guān)于Tomcat出現(xiàn)The origin server did not find a current represent
這篇文章主要介紹了關(guān)于Tomcat出現(xiàn)The origin server did not find a current representation for the target resourc...的問(wèn)題,感興趣的小伙伴們可以參考一下2020-08-08

