I am a freshman in IT and we did a quiz about linked lists and one of the questions was:
Linked List. Show the resulting list (redraw) after the following statements are executed.
So we have a doubly linked list list with each node having a character of the word "algorithm" (9 nodes), and where p1 references the node with value "l", p2 references the node with "i", and p3` the node with "t".
The statements to execute on this list are:
p2.next.prev = p2.prev; // (a)
p2.prev.next = p2.next; // (b)
p2.next = p3.next; // (c)
p3.next.prev = p2; // (d)
p3.next = p2; // (e)
p2.prev = p3; // (f)
list.next.next = p1.next; // (g)
p1.next.prev = list.next; // (h)
p1 = null; // (i)
System.gc(); // (j)
The answer I got was 'agortihm', but it was considered incorrect. The picture of the question indicates the correct answer.
I have been at it for 2 hours now, but I still can't get how they arrived at that correct answer. I keep arriving at 'agortihm' as an answer. Any help would be much appreciated thank you!

You correctly identified that the first six statements move the node "i" after node "t". We start with:
After (a)
p2.next.prev = p2.prev;:After (b)
p2.prev.next = p2.next;:After (c)
p2.next = p3.next;:After (d)
p3.next.prev = p2;:After (e)
p3.next = p2;:After (f)
p2.prev = p3;:The move of the node "i" has been completed. We can simplify the above drawing when we picture node "i" at the right of node "t". As these nodes are referenced by
p2andp3, also those move along:Where you went wrong
The next statements don't change anything, as they assign references that the target properties already have before the assignment takes place:
And the last assignment
p1 = null;only affects a variable, and not the list:And so the result is: "algortihm".
Note that the answer drawn on the image is not correct. For instance, it claims that the node "o" is followed by node "t". But none of the instructions mutates the node with "o", so its successor must be "r" as in the original list -- that
nextreference never changed.Verification
We could just write code to actually execute those statements and see what we get. Although your question is for Java, I choose here to add a runnable snippet (JavaScript) that executes the given statements: