title: How to make improve performance on my implement of this LinkedList?
body: <p>The problem is as follows</p>
  <blockquote>
  <p>A TrainComposition is built by attaching and detaching wagons from the
  left and the right sides, efficiently with respect to time used.</p>
  <p>For example, if we start by attaching wagon 7 from the left followed
  by attaching wagon 13, again from the left, we get a composition of
  two wagons (13 and 7 from left to right). Now the first wagon that can
  be detached from the right is 7 and the first that can be detached
  from the left is 13.</p>
  <p>Implement a TrainComposition that models this problem.</p>
  </blockquote>
  <p>This question may still be relevant in regards to how to solve ordering of a list of items.  Originally, I submitted an answer to such a problem posed during a Testdome exercise.  While the answer was logically correct, it lacked the performance requirements.  Therefore, I asked this question herein.</p>
  <p>My updated question, as follows:</p>
  <p>I have reviewed different collection types, but am stuck on how to improve performance over my implement of this LinkedList.</p>
  <p>The behaviour I need is that of LinkedList collection, but performance is weak.  Do you see any tweaks I could make to improve performance?  Perhaps there is another type of collection that gives function of linked list and improves performance.  I am still working this out.  Thanks for the help.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  public class TrainComposition
  {
  
      public TrainComposition()
      {
          Wagons = new LinkedList&lt;int&gt;();
      }
  
      private LinkedList&lt;int&gt; Wagons;
  
      public void AttachWagonFromLeft(int wagonId)
      {
          Wagons.AddFirst(wagonId);
      }
  
      public void AttachWagonFromRight(int wagonId)
      {
          Wagons.AddLast(wagonId);
      }
  
      public int DetachWagonFromLeft()
      {
          var wagon = Wagons.First.Value;
          Wagons.Remove(wagon);
          return wagon;
      }
  
      public int DetachWagonFromRight()
      {
          var wagon = Wagons.Last.Value;
          Wagons.Remove(wagon);
          return wagon;
      }
  
      public static void Main(string[] args)
      {
          TrainComposition tree = new TrainComposition();
          tree.AttachWagonFromLeft(7);
          tree.AttachWagonFromLeft(13);
          Console.WriteLine(tree.DetachWagonFromRight()); // 7 
          Console.WriteLine(tree.DetachWagonFromLeft()); // 13
      }
  }
  </code></pre>
  <p>A similar question was asked <a href="https://stackoverflow.com/questions/43135366/best-solution-for-the-train-composition">here</a>, but using java and not C#.  Therefore, the libraries differ and I think it is an unasked question.</p>
  <p><strong>EDIT</strong></p>
  <p>For clarity, please refer to the above stated <a href="https://www.testdome.com/for-developers/solve-question/9929" rel="nofollow noreferrer">Testdome</a> problem description.</p>
  <blockquote>
  <p>If it possible, take my code above, and plunk it into the Testdome client code edit box to run the tests and see results, as follows:</p>
  </blockquote>
  <blockquote>
  <ul>
  <li>Example case: Correct answer</li>
  </ul>
  </blockquote>
  <ul>
  <li>Several wagons: Correct answer</li>
  <li><strong>Performance test</strong> with a large number of wagons: Time limit exceeded</li>
  </ul>
  <p><strong>EDIT</strong></p>
  <p>Using a List, as follows, I also am unable to pass performance requirement:</p>
  <pre><code>    public TrainComposition()
      {
          Wagons = new List&lt;int&gt;();
      }
  
      private List&lt;int&gt; Wagons;
  
      public void AttachWagonFromLeft(int wagonId) // insert at index 0
      {
          Wagons.Insert(0, wagonId);
      }
  
      public void AttachWagonFromRight(int wagonId) // add item to last/end 
      {
          Wagons.Add(wagonId);
      }
  
      public int DetachWagonFromLeft() // remove first item (index = 0)
      {
          var wagon = Wagons[0];
          Wagons.RemoveAt(0);
          return wagon;
      }
  
      public int DetachWagonFromRight() // remove last item (index = count - 1)
      {
          var lastWagonIndex = Wagons.Count() - 1;
          var wagon = Wagons[lastWagonIndex];
          Wagons.RemoveAt(lastWagonIndex);
          return wagon;
      }
  </code></pre>
  <p><strong>LATEST UPDATE</strong></p>
  <p>Stay tuned, I am working on updating this question to provide codepen...</p>