/// <summary> /// Split a list by index /// Original list will end at the index /// The rest of list will be return as a new list /// </summary> /// <param name="list"></param> /// <param name="i"></param> /// <returns></returns> public static LList <T> Split(LList <T> list, int i) { if (list.IsEmpty() || list.Count() == 1) { return(null); } LList <T> result = new LList <T>(); var temp = list.FindByPosition(i); if (temp == null) { return(null); } result.First = temp.Next; if (i == list.Count() - 1) { result.Last = temp.Next; } else { result.Last = list.Last; } temp.Next = null; return(result); }
/// <summary> /// Reverse strategy 2 /// traverse from (first,last) , (first+1,last-1) ... swap each other's value /// </summary> public static void Reverse2(LList <T> list) { if (list.IsEmpty()) { return; } T temp; for (int i = 0; i < list.Count() / 2; i++) { temp = list.FindByPosition(i).Value; list.FindByPosition(i).Value = list.FindByPosition(list.Count() - 1 - i).Value; list.FindByPosition(list.Count() - 1 - i).Value = temp; } }