简体   繁体   中英

Void Method to Reverse Own Implementation of Java LinkedList

Whatever way it's possible, how can I do this? I need a method that modifies the actual list itself. I have tried doing this:

// Reverses this list.
public void reverse() {
    for (int i = 0, j = size - 1; i < size && j >= 0; i++, j--)
        set(i, get(j));
}

... but I failed. Halfway through it starts over and I'm just sucking. The output ends up being:

List:       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
Reversed:   [24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

How can I avoid this problem of repeating the numbers once it reaches the middle? Thanks.

Sir,

I managed to do this, you could may be add your own logic to reduce the for loops that i have used. But i hope it helps in a way.

    for(int i=0;i<list.size()/2;i++) {
        int val = list.removeLast();
        System.out.println("i:" + i + " val " + val);
        reverseList.add(val);
    }

    for(int i=list.size()/2;i<list.size();i++) {
        int val = list.removeLast();
        System.out.println("i:" + i + " val " + val);
        reverseList.add(val);
    }

    for(int i=list.size()/4;i<list.size();i++) {
        int val = list.removeLast();
        System.out.println("i:" + i + " val " + val);
        reverseList.add(val);
    }

    for(int i=list.size()/6;i<list.size();i++) {
        int val = list.removeLast();
        System.out.println("i:" + i + " val " + val);
        reverseList.add(val);
    }

    for(int i=list.size()/8;i<list.size();i++) {
        int val = list.removeLast();
        System.out.println("i:" + i + " val " + val);
        reverseList.add(val);
    }

    for(int i=list.size()/10;i<list.size();i++) {
        int val = list.removeLast();
        System.out.println("i:" + i + " val " + val);
        reverseList.add(val);
    }

    for(int i=list.size()/12;i<list.size();i++) {
        int val = list.removeLast();
        System.out.println("i:" + i + " val " + val);
        reverseList.add(val);
    }

As you go through the list, you are overwriting the first half of the values with values from the end, thereby losing those early values. When you get to the second half of the list, the original values in the first half are no longer there!

Try using swap logic (exchanging two elements at each iteration) and going only halfway through the list:

public void reverse() {
    int half = size / 2;
    for (int i = 0; i < half; i++) {
        int j = size - 1 - i; // position of matching element at the other end
        T item = get(i); // T is the type of data stored in the list
        set(i, get(j));
        set(j, item);
    }
}

Note that you don't need to swap the middle element with itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM