Linked list reverse for Java

Source: Internet
Author: User

Java version linked list reverse

Define data Structures:

/** * Data structure of the linked list */class Linkedlistarray {    /**     * value */    Object value;    /**     * Next node */    Linkedlistarray next = null;    public void SetValue (Object value) {        this.value = value;    }    public void Setnext (Linkedlistarray next) {        this.next = next;    }    Public Object GetValue () {        return value;    }    Public Linkedlistarray GetNext () {        return next;    }}
Link List Inverse method:

/** * Created by Hyson on 15/7/14. */public class Listreverse {public static void main (string[] args) {Linkedlistarray llr1 = new Linkedlistarray        ();        Llr1.setvalue ("Chain");        Linkedlistarray LLR2 = new Linkedlistarray ();        Llr2.setvalue ("table");        Linkedlistarray LLR3 = new Linkedlistarray ();        Llr3.setvalue ("inverse");        Linkedlistarray LLR4 = new Linkedlistarray ();        Llr4.setvalue ("place");        Llr1.setnext (LLR2);        Llr2.setnext (LLR3);        Llr3.setnext (LLR4);        Print (LLR1);        LLR1 = reverse (LLR1);    Print (LLR1);        public static void print (Linkedlistarray head) {linkedlistarray tmp = head;            while (tmp! = null) {System.out.print (Tmp.getvalue ());        TMP = Tmp.next;    } System.out.println (); }/** * List reverse * @param listhead Header * @return Reverse list * */public static Linkedlistarray reverse (Linkedlis         Tarray listhead) {linkedlistarray lastlist,//remaining linked list       Tmplist,//Temporary list result;//results linked list reference lastlist = Listhead; Tmplist = result = null;//The staging list is initialized with the result set to empty while (lastlist! = null) {tmplist = lastlist.next;//The staging list is staged in addition to the first node The remaining list Lastlist.next = result;//puts the first node into the remaining list header result = lastlist;//reference to the first position of the list lastlist = tmp    list;//remaining list reference re-points to the remaining list of persisted lists} return result; }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linked list reverse for Java

Related Article

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.