text: Q:
  
  Reversing a linked list in Java without changing the original
  
  I am trying to reverse a linked list which works, but when I try to print the original, it fails (only prints the head). My question is, why would reversing affect the original. Below is my code. LinkedList is my own class, so is Node. Before reversing, if I try to print my list, that works.
  public static void main(String[] args) {
      LinkedList list;
      ...
      Node head = list.getHead();
      Node rev = reverse(head);
      Node temp = rev;
      while (temp != null) {
          System.out.println(temp);
          temp = temp.next;
      }
  
      temp = head;
      while (temp != null) {
          System.out.println(temp);
          temp = temp.next;
      }
  }
  
  private static reverse(Node head) {
       // Reversing the linked list
  }
  
  EDIT::
  This seems to be a Java thing. Java passes an object by reference. When I pass head as a parameter, it's passed by reference and any change made to it is reflected in the calling function.
  Doing Node h = head and then passing h as a parameter won't work either since h will be the same object as head.
  The only option I can think of is to create a new object, copy the linked list over and pass that as a parameter.
  My question becomes, is there a better solution?
  
  A:
  
  To understand it, picture your list looks like this
  list (list.head =a) --> a (a.next=b) --> b (b.next= c) -> c (c.next = null)
  
  If you get the head, then you are getting object 'a'.
  Then you are modifying object 'a'.
  So you can see you are editing the list by doing this.
  What you need to do is:
    Get the head 
    Create a copy
    Reverse the copy
    Get the next item
    Copy it
    Reverse it
    Join it to the last
    And so on
  Since you are using your own classes not java collections classes, the easiest way is for you to make sure that reverseNode() only edits a copy of the one you pass it, and returns the copy.
  First make sure your Node class has a constructor that copies another Node, then do something like this:
  private static Node reverse(Node original) 
  {
    Node retval = new Node(original);
    // or you could use clone () as Bhavik suggested, if your Node class implements it
    // modify retval 
    // I haven't shown code to reverse it as I assume you already have that and you didnt specify if it was a bidirectional list or just one direction.
  
    return retval;
  }
  
  Or you might add a static method in your Node class that constructs a new node that is reversed:
  static Node createReverse(Node n) 
  {
      return new Node(n.data,n.next,n.prior);
  }
  
  Or a non static method of the node class which returns a reversed copy of itself;
  Node createReverse()
  {
     return new Node(this.data,this.next,this.prior);
  }
  
  But you should consider this can get very ugly because your copies will still have pointers pointing into the existing list!
  A better technique might be to create a new empty list, and then start from the end of your original, make a copy, and add it to the start of your new list. 
  You can use recursion to do this but might easily run out of memory.
  But rather than do this manually, you might look at the java.util packages and switch to using one of their LinkedList and list item types. These classes have already solved all the problems with doing this stuff.
  Then you could (if you need to keep the original list unmodified):
    - Make a copy of your entire list.
    - reverse the copy as below
  If you don't care about keeping the original, then just use this method(below) on your list, no need then to make a copy.
  From java.util.Collections:
  Collections.reverse(List a_list);
  
  The Collections class will choose an efficient way to reverse the list, depending on whether it is bidirectional, single directional, etc.
  
  