Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Something like this? (I tried to base it on your code):</p> <pre><code>LinkedList&lt;BD&gt; list = new LinkedList&lt;BD&gt;(b[arg].Values); LinkedListNode&lt;BD&gt; node = list.Last; LinkedListNode&lt;BD&gt; terminator = null; while (node != null &amp;&amp; node != terminator) { if (IDs.Contains(node.Value.DocumentVersionId)) { LinkedListNode&lt;BD&gt; tempNode = node; node = node.Previous; list.Remove(tempNode); list.AddFirst(tempNode); if (terminator == null) terminator = tempNode; } else { node = node.Previous; } } </code></pre> <p>This piece of code should move your "DocumentVersionId-matched" nodes to the front of the linked list.</p> <p>Below is an example with simple integers to demonstrate how it works:</p> <pre><code>List&lt;int&gt; specials = new List&lt;int&gt; { 1, 4, 5, 7 }; List&lt;int&gt; source = new List&lt;int&gt; { 1, 2, 3, 4, 5, 6, 7, 8 }; LinkedList&lt;int&gt; list = new LinkedList&lt;int&gt;(source); LinkedListNode&lt;int&gt; node = list.Last; LinkedListNode&lt;int&gt; terminator = null; while (node != null &amp;&amp; node != terminator) { if (specials.Contains(node.Value)) { LinkedListNode&lt;int&gt; tempNode = node; node = node.Previous; list.Remove(tempNode); list.AddFirst(tempNode); if (terminator == null) terminator = tempNode; } else { node = node.Previous; } } </code></pre> <p>The result linked list will contain:<br> 1, 4, 5, 7 (the specials at the beginning of the linked list), 2, 3, 6, 8</p> <p>An endless loop should be impossible.</p> <p><strong>Answer edits:</strong><br> - <em>node = list.First</em> to <em>node = list.Last</em><br> - added an example with integers</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload