/// <summary> /// More Efficient solution /// </summary> /// <param name="rootF"></param> /// <param name="rootS"></param> /// <returns></returns> ILinkedNode <int> MergeLists(ILinkedNode <int> rootF, ILinkedNode <int> rootS) { if (rootF == null) { return(rootS); } if (rootS == null) { return(rootF); } int res = rootF.CompareTo(rootS.Data); if (res < 0) { rootF.Next = MergeLists(rootF.Next, rootS); return(rootF); } rootS.Next = MergeLists(rootS.Next, rootF); return(rootS); }
void Merge(ILinkedNode <int> rootF, ILinkedNode <int> rootS) { ILinkedNode <int> merged = null; ILinkedNode <int> lastmerged = new LinkedNode <int>(); ILinkedNode <int> first = rootF; ILinkedNode <int> second = rootS; while (first != null || second != null) { if (first == null) { lastmerged = addValToGener(lastmerged, second); second = second.Next; continue; } if (second == null) { lastmerged = addValToGener(lastmerged, first); first = first.Next; continue; } while (second != null && first != null) { int res = first.CompareTo(second.Data); if (res >= 0) { ILinkedNode <int> nexts = second.Next; lastmerged = addValToGener(lastmerged, second); second = nexts; } else if (res < 0) { ILinkedNode <int> nextf = first.Next; lastmerged = addValToGener(lastmerged, first); first = nextf; } } } }