Reverse order of Java single linked list

Source: Internet
Author: User

First you need a node to implement a single linked list.

Package Com.lxk.linkedList.oneWay;

/**
 * @author lxk on 2017/8/1
/public class node<k, v> {
    private final K key;
    private V value;
    Private Node<k, v> next;

    Public Node (K key, V value, Node<k, v> next) {
        this.key = key;
        This.value = value;
        This.next = next;

    Public K Getkey () {return
        key;
    }

    Public v. GetValue () {return
        value;
    }

    public void SetValue (V value) {
        this.value = value;
    }

    Public node<k, V> GetNext () {return
        next;
    }

    public void Setnext (Node<k, v> next) {
        this.next = next;
    }
}

Then, it is how to achieve reverse order.

The first kind of implementation, simple and good understanding.

    /**
     * Reverse order single linked list
     *
     * @param head single link
     /public
    static Node<integer, integer> reverse1 (node< Integer, integer> head) {
        Node<integer, integer> reverse = null;
        Node<integer, integer> current = head;
        while (current!= null) {
            Node<integer, integer> temp = current;
            Current = Current.getnext ();
            Temp.setnext (reverse);
            Reverse = temp;
        }
        return reverse;
    }

The second implementation. recursion, not understanding.

    /**
     * Reverse order single linked list (recursive)
     *
     * @param head single link/
     public
    static Node<integer, integer> Reverse2 (Node <integer, integer> head) {
        //when empty or when this node is the end node
        if (head = = NULL | | | head.getnext () = null) {return head
            ;
        }
        Node<integer, integer> reversedhead = Reverse2 (Head.getnext ());
        Gets the previous next node, which points to its own
        head.getnext (). Setnext (head);
        Destroys itself to point to the next node
        head.setnext (null);
        Layers are passed to the top return
        reversedhead;
    }

Here are 2 more methods.

One is the initialization of a single linked list. One is the output single linked list.

    /**
     * To obtain one-way linked list (head interpolation generated one-way linked list-later in the head of the linked list)/public
    static Node<integer, integer> getonewaylinkedlist (int length) {
        Node<integer, integer> temp = null;
        for (int i = 1; I <= length; i++) {
            //head interpolation: First come at the end of the chain
            temp = new Node<> (i, I, temp);
        }
        return temp;
    }
    /**
     * Output one-way linked list
     *
     * @param linkedlist one-way linked list, the position of the chain header begins.
     *
    /public static void Forlinkedlist (Node<integer, integer> linkedlist) {
        StringBuilder sb = new StringBuilder ();
        Sb.append ("{");
        while (LinkedList!= null) {
            sb.append ("[k:"). Append (Linkedlist.getkey ()). Append ("V:"). Append ( Linkedlist.getvalue ()). Append ("]");
            LinkedList = Linkedlist.getnext ();
        }
        Sb.append ("}");
        System.out.println (Sb.tostring ());
    }
And then there's the main method, actually running, looking at the results.
        Node<integer, integer> LinkedList = getonewaylinkedlist (6);
        Forlinkedlist (LinkedList);
        Node<integer, integer> node = reverse1 (LinkedList);
        Node<integer, integer> node = Reverse2 (LinkedList);
        Forlinkedlist (node);

Then it's the result of the operation.


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.