title: Swap group of nodes in a doubly linked list
body: <p>I came across this problem in which you should swap a group of nodes in a doubly linked list. For example: for the list <code>1 &lt;-&gt; 2 &lt;-&gt; 3 &lt;-&gt; 4 &lt;-&gt; 5 &lt;-&gt; 6 &lt;-&gt; 7 &lt;-&gt; 8</code> and the given intervals 2-4 and 6-7 you should swap the nodes in the interval as a group with the other nodes in the interval, getting the output <code>1 &lt;-&gt; 6 &lt;-&gt; 7 &lt;-&gt; 5 &lt;-&gt; 2 &lt;-&gt; 3 &lt;-&gt; 4 &lt;-&gt; 8 </code>. I have the idea to treat the whole group as a single node, meaning I should connect 6.prev with 1 and 7.next with 5, but because this is a doubly linked list, I find it hard to think of a solution that would change all the needed pointers successfully. Can someone help me and explain to me how should this be done? Thanks.</p>