【数据结构】【线性表】逆序链表

       线性表逆序链表使用Java实现,实现思路和C语言中的实现思路一模一样,不赘述;和顺序链表不同的是,逆序链表不需要“尾指针”,这样就少了tail节点的定义;另外逆序链表中的head节点不能存储数据,它用来引导每个节点的插入位置。

一、代码实现

import java.util.Scanner;
class Node{
        int data;
        Node next=null;
}
public class ReverseLinkedList{
        public static void main(String args[]){
                Scanner scanner=new Scanner(System.in);
                int total=scanner.nextInt();
                Node head=new Node();
                Node p=null;
                for(int i=0;i<total;i++){
                        p=new Node();
                        p.data=scanner.nextInt();
                        p.next=head.next;
                        head.next=p;
                }
                output(head);
        }
        public static void output(Node head){
                Node p=head.next;
                while(p!=null){
                        System.out.print(p.data+" ");
                        p=p.next;
                }
                System.out.println();
        }
}

 二、测试用例

使用之前的MyRandom类:

import java.util.Random;

public class MyRandom {
        public static void main(String args[]){
                int[] array=new int[1024];
                for(int i=0;i<1024;i++){
                        array[i]=i;
                }
                System.out.println("1024");
                for(int i=0;i<1024;i++){
                        System.out.print(array[i]+" ");
                }
                System.out.println();
        }
}

 测试结果:




从结果中就能够看出来为什么叫做“逆序链表”了,顺序输入了1024个整数,但是输出的时候却是逆序的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值