The starting point is a list or array, for example:
1 {A, 4, 9} 2 {B, 6, 1} 3 {C, 1, 3} 4 {D, 3,10} 5 {E, 7, 2} 6 {F, 4, 2} 7 {G, 9, 8} 8 {H,10, 5} 9 {I, 7, 5} 10 {J, 6, 8}
If we can change this to a list or array, for example:
1 {C, 1, 3} 2 {F, 2, 4} (nodes swapped) 3 {D, 3,10} 4 {A, 4, 9} 5 {I, 5, 7} (nodes swapped) 6 {B, 6, 1} 7 {E, 7, 2} 8 {J, 8, 6} (nodes swapped) 9 {G, 9, 8} 10 {H,10, 5}
which is ordered by node1, then we can read this list or array as a linked list:
start with item 1: {C, 1, 3} read node2: 3 skip to item 3: {D, 3,10} read node2: 10 skip to item 10: {H,10, 5} ... skip to item 6: {B, 6, 1} read node2: 1 end of list result: CDHIEFAGJB
Creating this second version of the list can be done in place by replacing items in the list or by copying items to a new list (if you have a space).
The only thing you need to pay attention to is that sometimes you have to change both nodes. When re-ordering on the spot, it may look like this:
look at item 1: {A, 4, 9} if item 4 has a node1 different from 4, swap item 1 and 4 else, change item 1 to {A, 9, 4} and swap item 1 and 9 (-> item 1 and 4 swapped) while current item is already in-place, skip to next (-> stay at item 1) look at new item 1: {D, 3,10} if item 3 has a node1 different from 3, swap item 1 and 3 else, change item 1 to {D,10, 3} and swap item 1 and 10 (-> item 1 and 3 swapped) while current item is already in-place, skip to next (-> item 1 is now {C, 1, 3}, so skip to item 2) ...
When creating a new list or array, this should be even simpler:
look at item 1: {A, 4, 9} if new list has no item 4, copy item 1 as item 4 to the new list else, change item 1 to {A, 9, 4} and copy as item 9 to the new list move to next item
As you can see, there is no need to iterate over the list several times; each element is replaced or copied once, and its purpose is determined by its node1 or node2.
UPDATE
I just realized that the number of steps for ordering items can be (much) more than described above. If, for example, you start by moving {A, 4.8} to location 4 and {B, 5.9} to location 5, and then you come across {C, 4,5}, you get stuck. Then you have to swap {C, 4,5} with one of the other two elements, swap in another element and move them into place. This new place can already be taken, and so on, so there would be no way to find out which of the two options is the lesser evil. In the worst case, the number of swaps would be close to N 2 .
UPDATE 2
The above problem, of course, can be solved by storing the items in a doubly linked list. When you come across, for example, {A, 4,8}, you store {A, 8} as element 4 and {A, 4} as element 8, and then for {B, 5,9} you store {B, 9 } element 5 and {B, 5} as point 9, and then for {C, 4,5}, you add to the already saved elements, so that element 4 becomes {A, 8, C, 5}, and element 5 becomes {B, 9, C, 4}. Then you can cross the doubly linked list in both directions. This will increase the work that needs to be done and the space used, but it is still linear for the number of items in the list.