public void MergeWith(LinkedListAgain otherList) { Node myCurrent = head; Node otherCurrent = otherList.head; Node newHead = null; Node newTail = null; //pick one from each list and add both per iteration while (myCurrent != null || otherCurrent != null) { if (myCurrent != null) { InsertIntoList(ref newHead, ref newTail, myCurrent); myCurrent = myCurrent.Next; } if (otherCurrent != null) { InsertIntoList(ref newHead, ref newTail, otherCurrent); otherCurrent = otherCurrent.Next; } } this.head = newHead; this.tail = newTail; }
public static void Test() { LinkedListAgain list = new LinkedListAgain(); list.Add(2); list.Add(1); list.Add(4); list.Add(3); list.Add(5); //LinkedListAgain list2 = new LinkedListAgain(); //list2.Add(2); //list2.Add(4); //list2.Add(6); //list2.Add(8); //list.MergeWith(list2); list.Print(); list.SwapElements(); list.Print(); list.Add(6); list.Print(); }