码迷,mamicode.com
首页 > 编程语言 > 详细

java实现单链表逆置

时间:2016-04-14 01:04:46      阅读:489      评论:0      收藏:0      [点我收藏+]

标签:

 1 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 }

 

java实现单链表逆置

标签:

原文地址:http://www.cnblogs.com/lindaZ/p/5389335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!