标签:
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 }
标签:
原文地址:http://www.cnblogs.com/lindaZ/p/5389335.html