java实现单链表逆置

class LNode
 2 {
 3     public LNode next;
 4     public int data;
 5 }
 6 /*逆置链表*/
 7 class Nizhi
 8 {
 9     private static LNode head = new LNode();;
10     private static LNode node;
11     private static LNode tail;
12     private static int index;
13     private static LNode newhead = new LNode();
14     public static void main(String[] args){
15         int[] nums = {1,2,3,4,5,6,7,8,9,10}; 
16         head.data = nums[0];
17         tail = head;
18         createLine(nums);
19         System.out.println("——————————————————链表顺序打印———————————————————");
20         printLine(head);
21         nizhi();
22         System.out.println("——————————————————链表逆置打印———————————————————");
23         printLine(newhead);
24     }
25 
26     private static void createLine(int[] nums){
27         while (index<10)
28         {
29             node = new LNode();
30             tail.next = node;
31             node.data = nums[index];
32             node.next = null;
33             tail = node;
34             index ++;
35             
36         }
37     }
38 
39     private static void printLine(LNode head){
40         node = head;
41         while(node!=null&&node.next!=null){
42             node = node.next;
43             System.out.println(node.data);
44         }
45     }
46 
47     private static void nizhi(){
48         
49         node = head.next;
50         //遍历原链表结点,头插法到新头结点
51         while (node!=null)
52         {
53             LNode temp = new LNode();
54             temp.data = node.data;
55             //node.next = node.next.next;
56             temp.next = newhead.next;
57             newhead.next = temp;
58             
59             System.out.println("temp.data="+temp.data);
60             node = node.next;
61         }
62     }
63 }

 

1、节点(Node)属性:

 

 

2、反转的递归实现:

 

3、测试代码:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值