Understanding the reversal of a single-linked list (Java implementation)

Source: Internet
Author: User

The requirements are simple, enter a list, invert the linked list, and output the table header of the new list.
?? Reverse linked list is 2 methods (recursive method, traversal method) implementation, the interviewer's favorite algorithm is nothing more than the Fibonacci sequence and single-linked list inversion, recursive method to achieve a more elegant chain list reversal, but for the students do not understand the recursion is still difficult to understand.

Recursive method

In general, recursion starts with the last node and replaces the pointer order in the process of the stack.

To make it easy to understand, we use the 1->2->3->4 list as a demonstration. The effect of the output is 4->3->2->1

First define node:

publicstaticclass Node {    publicint value;    public Node next;    public Node(int data) {        this.value = data;    }}

The reversal method is as follows:

publicreverse(Node head) {    ifnull || head.nextnull)        return head;    Node temp = head.next;    reverse(head.next);    temp.next = head;    head.nextnull;    return newHead;}

Recursion is essentially a system to help you stack the process, the system in the stack when the site will be retained.

Let's see how it is. A recursive process: 1->2->3->4

  • The program arrives at node Newhead = reverse (head.next);
  • We assume that at this point we are recursive to 3 nodes, at which point head=3 nodes, temp=3 nodes. Next (actually 4 nodes)
  • Execute node newhead = reverse (head.next); the incoming head.next is a 4 node, and the returned Newhead is 4 nodes.
  • Next up is the stack process.
  • The program continues execution Temp.next = head is equivalent to 4->3
  • Head.next = NULL The pointer that points to the 4 node of the 3 node is broken off.
  • Returns the head node of the new linked list Newhead

    Note: When Retuen, the system will restore the site when the 2 node is stacked, the head=2 node at this point, the temp=2 node. Next (3 nodes), and then do the above. Finally, complete the entire list of flips.

    Traversal method

    The traversal method is to replace the pointer order during the traversal of the linked list

    First on the code:

    publicstaticreverseListnullnull;whilenull) {    next = node.next;    node.next = pre;    pre = node;    node = next;}return pre;}
    It's still 1->2->3->4.
  • Prepare two empty nodes the pre is used to save the previous node, and next to make the temporary variable.
  • 1 nodes at the time of node traversal of the head node.
    • Next = 1 nodes. Next (2 nodes)
    • 1 nodes. Next=pre (NULL)
    • Pre = 1 nodes
    • node = 2 node
  • Make the Next loop node=2 node.
    • Next = 2 nodes. Next (3 nodes)
    • 2 nodes. Next=pre (1 nodes) = = Complete 2->1
    • Pre = 2 nodes
    • node = 3 node
  • To cycle ...

Understanding the reversal of a single-linked list (Java implementation)

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.