简体   繁体   中英

How do I reverse the order of this implementation of a Linked List?

I have a class called LinkStrand which functions very similarly to a Linked List. It has toString() , size() , append() , next() , and value() , but NOT a previous() method. I am trying to write code that reverses the order of the nodes, as well as the string within each node. In order to make it easier on myself in a few of the other methods I've had to write, I got rid of the next node requirement in constructing a node. Here's what the Node class looks like:

private class Node {
    public Node myNext;
    public String myData;

    Node(String value) {
        myData = value;
        //myNext = next;
    }   
}

My .reverse() method currently reverses all of the strings within the nodes individually, but does not reverse the order of the nodes themselves. It is copied below:

public IDnaStrand reverse() {
    if (this == null)
        return this;
    Node prevStrand = null;
    Node thisStrand = myHead;
    String revString;
    LinkStrand val = new LinkStrand();
    while (thisStrand != null){
        Node hold = thisStrand.myNext;
        if (revSave.containsKey(thisStrand.myData)){
            revString = revSave.get(thisStrand.myData);
            val.append(revString);
            //System.out.println("Val is: " + val);
        }
        else{
            revString = reverseStr(thisStrand.myData);
            val.append(revString);
            //System.out.println("Val is: " + val);
            revSave.put(thisStrand.myData, revString);
        }
        thisStrand.myData = revString;
        thisStrand.myNext = prevStrand;
        prevStrand = thisStrand;
        thisStrand = hold;
    }
    return val;
}

I've been trying to come up with some kind of way to reverse the node order, but I'm drawing a blank. Does anyone have any idea how I might go about it?

Thanks!

If you are allowed to modify IDnaStrand and LinkStrand , add a method prepend(Node n) . Then, as you iterate through the list, just prepend each node.

If you can't modify your classes, save the Node s to an array in reverse order ( nodeArray[size-1] , nodeArray[size-2] , ...) then create a new LinkStrand going through the array in order. Alternatively you could load the array in order, then create the LinkStrand in reverse order.

example:

thisStrand = myHead;
int size = 0;
while(thisStrand != null){
    thisStrand = thisStrand.myNext;
    size++;
}
Node[] nodeArray = new Node[size];
thisStrand = myHead;
for(int i = size-1, i < 0; i--) {
    nodeArray[i] = thisStrand;
}

Now you have the array, just load it into a new list! Of course adding a prepend method would be better, just have the class do

newElement.myNext = MyHead;
MyHead = newElement;

Create a new instance loop through the original and insert into the new instance at position 0, then return it.

Other way would be to sort it, but can't see anything in your question that would indicate it is currently sorted.

Let's say your list looks like this:

A -> B -> C -> D -> E

I'll shorten the notation because I'm going to write this a lot:

A B C D E

Let's take 3 variables. I'll show their whole lists, so you can see what is going on. The first item is always the node stored in one of the variables

list: null
curr: A B C D E
next: B C D E

Set A's next-pointer to the value of list (null). It's now the end of the list.

list: null
curr: A
next: B C D E

Now, move to the next:

list: A
curr: B C D E
next: C D E

You can see what's going to happen. Continue as we started: set the next-pointer of curr to list :

list: B A
curr: B A
next: C D E

Advance:

list: B A
curr: C D E
next: D E

Again:

list: C B A
curr: C B A
next: D E

And so on...

Pseudocode is rather simple:

list = null
curr = original_list

while next != null
    next = curr->next
    curr->next = list
    list = curr
    curr = next
end

All this is really doing is taking each node from the head of the list and making it the head of the other list. This has the effect of reversing the order. My answer is perhaps a little long-winded, but that's all you do.

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