Reverse output of the Java version of a one-way list

Source: Internet
Author: User

The one-way linked list is output in reverse order, with three ways:

A. Iterate through the linked list, store the contents of each node in an array, and then reverse the output array (the simplest way)

B. Using stacks to reverse output

C. Reverse the list directly and then output (the method used in this article)

First introduce the algorithm:

1). If the list is empty or if there is only one element, return directly;

2). Set two adjacent pointer p,q. The node pointed by P as the successor of the Q point node;

3). Repeat 2) until q is empty

4). Adjust the head of the chain and the end of the chain list

Example: Take reverse a->b->c->d as an example, as shown below




Package Com.moluo.shujujiegou;public class Node {/** * @author: Walker */private int data;private Node next;public int GETDA Ta () {return data;} public void SetData (int data) {this.data = data;} Public Node GetNext () {return next;} public void Setnext (Node next) {This.next = Next;}}

Package Com.moluo.shujujiegou;import Java.util.scanner;public class Linklist {public Node head;public void createlinklist (int []x) {//Create a linked list node pnew;//define a new node node Ptail = new node (); head = ptail;for (int i = 0; i < X.leng Th i + +) {pnew = new Node ();p new.setdata (X[i]);p tail.setnext (pnew);p tail = pnew;}} public void Displaylinklist () {////Positive order output list of all contents node node = Head.getnext (); while (node! = null) {System.out.print (node.getd ATA () + "--->"); node = Node.getnext ();} SYSTEM.OUT.PRINTLN ("null");} public void Reverselinklist () {//Reverse output All contents of the list if (head = = NULL | | head.getnext () = null) {///When the list has only one head node or only one node, reverse or the original chain Table, so go directly back to return;} else {Node p = head.getnext (); Node q = Head.getnext (). GetNext ();p. Setnext (null);//The next of the first node is empty, otherwise a ring node temp = null;while (q! = null) {temp = Q.ge Tnext (); Q.setnext (p);p = Q;q = temp;} if (q = = null) {Head.setnext (p); q = null;}}} public static void Main (string[] args) {linklist linklist = new linklist (); Scanner input = new Scanner (system.in); int n = inPut.nextint (); int [] x = new int [N];for (int i = 0; i < n; i + +) {X[i] = i;} Linklist.createlinklist (x); linklist.displaylinklist (); Linklist.reverselinklist (); Linklist.displaylinklist ();}}

Output Result:

--->1--->2--->3--->4--->5--->6--->7--->8--->9--->null9--->8--->7---> 6--->5--->4--->3--->2--->1--->0--->null


Reference: http://blog.csdn.net/niuer09/article/details/5961004


Respect copyright, reprint please indicate this article link

Welcome to follow the public number (Xingzhemoluo), common communication programming experience, scan the following QR code can be;



Reverse-order output of the Java version of a one-way list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.