</pre><pre name= "code" class= "java" >package com.ckw.mianshi;/** * Java implements reverse order of a single list * @author Administrator * */p Ublic 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;}}
Test results:
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java implements reverse order of single-linked list