欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java实现单链表之逆序

时间:2021-05-22 08:42:16|栏目:JAVA代码|点击:

下面一段代码准确的介绍了java实现单链表逆序,具体内容就不做详解了,有需要的朋友可以直接拷贝了

package com.ckw.mianshi;
/**
 * java 实现单链表的逆序
 * @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;
 }
}
测试结果:
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0

以上是java实现单链表逆序的代码,希望大家能够喜欢。

上一篇:Java中char[]输出不是内存地址的原因详解

栏    目:JAVA代码

下一篇:基于Java 数组内存分配的相关问题

本文标题:java实现单链表之逆序

本文地址:http://www.codeinn.net/misctech/126693.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有