# Switch position of nodes on list

• April 5th, 2012, 08:09 AM
Neobs
Switch position of nodes on list
I have a Generic Linked list in Java.
Code :

```class Node{ T info; Node next;   Node (T x){ info = x; next = null; } }```

I want to switch positions of two nodes on a list. Where a was I want to put b and where b was I want to put a.
Code :

```  public void switcherT a, T b){ // Stuck. }```

• April 5th, 2012, 09:37 AM
Norm
Re: Switch position of nodes on list
To figure out the logic for this kind of problem I take piece of paper and draw a linked list with several nodes on it. Then work out the logic for removing a node, remember where it was, remove the second node, put the first node in the list where the second node was and then putting the second node into the list where the first node was.
• April 5th, 2012, 10:31 AM
Neobs
Re: Switch position of nodes on list
Thanks for mocking me on both my threads...
• April 5th, 2012, 10:38 AM
Norm
Re: Switch position of nodes on list
You are mistaken. What I suggested is the way that I would solve the problem. Before you write any code you must understand what the code is supposed to do. Drawing diagrams on paper of the before and after condition and then working out how to do the steps to get from the before to the after condition is how you get to understand the problem. You must do those steps.

If you have already done that step and are now ready to write the code, can you explain which step in the list of steps the program must do that you are having problems with?
• April 9th, 2012, 07:10 AM
KevinWorkman
Re: Switch position of nodes on list
Quote:

Originally Posted by Neobs
Thanks for mocking me on both my threads...