views:

211

answers:

4

Linked-List: Mirror

Consider the following private class for a node of a singly-linked list of integers:

private class Node{
public int value;
public Node next;
}

A wrapper-class, called, ListImpl, contains a pointer, called start to the first node of a linked list of Node.

Write an instance-method for ListImpl with the signature:

public void mirror();

That makes a reversed copy of the linked-list pointed to by start and appends that copy to the end of the list. So, for example the list:

start 1 2 3

after a call to mirror, becomes:

start 1 2 3 3 2 1

Note: in your answer you do not need to dene the rest of the class for ListImpl just the mirror method.

+1  A: 

What is your question? This looks like an exercise problem. Are you looking for an optimized solution? One way would be to just iterate over the list and add elements to a stack and finally add them as nodes after the iteration.

Fazal
That sounds like an n^2 solution - iterate over the list and add to a stack, then iterate over the stack? How about just build a linked list as you iterate over, then attach the head of 3-2-1 to the tail of 1-2-3.
Shakedown
@Shakedown: It does not seem like an n^2-solution to me. Iterating over the list takes linear time. For each node in the list, its value is pushed onto the stack in constant time. The time required to build the full stack is consequently linear. Pushing all the values off of the stack takes linear time. For each value, adding it to the end of the list can be done in constant time. The time required to add the new nodes to the list is consequently linear. Therefore, the entire operation is linear.
Matthew T. Staebler
@Aeth: Yes you're right - my mistake!
Shakedown
A: 

You can make use of this approach:

Algorithm findLinkedListMirror
  If list does not exist 
    return

  Let start and end be pointers to type Node
  Position start to the first node of the list
  Position end to last node of the list

  While(start != end.next)
    Add a new node next to end with value start.data
    start = start.next
  End While

End
codaddict
Positioning pointer to last node would need iteration anyways as its a singly linked list
Fazal
+1  A: 
public void mirror() {
    if (start != null) {
        Node prev = null;
        Node p = start;
        Node q = null;
        while (p != null) {
            Node n = new Node();
            n.value = p.value;
            n.next = q;
            q = n;
            prev = p;
            p = p.next;
        }
        prev.next = q;
    }
}
Maurice Perry
Does `n.next = q; q = n;` create a self-linked node?
Marcus Adams
No. It just links the new node to the previous one
Maurice Perry
+1  A: 

Since Maurice provided a looping solution, I will provide a recursive solution.

void mirror()
{
    if (start == null) return;
    mirrorSublist(start);
}

// returns the last node of the mirrored sublist
Node mirrorSublist(Node firstOfSublist)
{
    Node lastOfSublist = new Node();
    lastOfSublist.value = firstOfSublist.value;
    lastOfSublist.next = null;
    if (firstOfSublist.next == null)
    {
        firstOfSublist.next = lastOfSublist;
    }
    else
    {
        Node secondToLastOfSublist = mirrorSublist(firstOfSublist.next);
        secondToLastOfSublist.next = lastOfSublist;
    }
    return lastOfSublist;
}
Matthew T. Staebler