Flip of one-way linked list

Source: Internet
Author: User

One-way linked list flip, before the problem to think too simple, think as long as the data field flip over on it, the result is the basket of large dipper, the following a simple example illustrates:

Suppose there are N men standing in a row, now to put the order of the N individual station upside down, then can not just the n person's height upside down, but to the position of each person upside down, the first person to stand in the end, the second person stand the penultimate, and so on.

In order to verify the correctness of the program, this time we can not print the node data, but to print the node. When the Java program runs, the JVM opens up memory space for the program, each node in the stack has a saved location, it should be noted that every time the program runs a node in the memory of the location is not a certain, so the method test must be in a run to test, This is also the problem that I have been struggling with before, why print the node element is right, but the knot point is different.

Here's how I realize the flip of the linked list, and look at the fake flip before I explain the correct method:

Java code

public void Wrongturn () {for  
    (int i=1;i<size (); i++) {  
        Insert I,get (Size ()). obj);  
        Delete (size ());  
    }  
}

The Insert method here is described in my previous blog

http://cq520.iteye.com/blog/1860061, this approach means that all the nodes are copied first, and then inverted into the list, each inserted after the last one more out of the node removed, If you only print the current list element it is easy to be blinded by this method, but obviously, this approach can achieve the reverse effect, but the inverse of the node is all changed, as you change a bunch of people to do this thing, they operate the memory address is not the same block.

Here's how to do the right thing:

This is a single linked list:

1. Put the tail node in front of the first node and put the first node backwards:

2. Insert the current last node into the next location in the 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.