static public void Main() { int[] elem = { 1, 2, 3, 4, 5 }; MyLinkedList <int> list = new MyLinkedList <int>(elem); SwapPairwise(list); list.Print(); }
static public MyLinkedList <int> Merge(MyLinkedList <int> a, MyLinkedList <int> b) { a.Print(); b.Print(); //vamos arevisar cada lista y poner los elementos en la lista que tenga el elemento m[as pequeli MyNode <int> x; if (b.First.Value <= a.First.Value) { x = b.First; b.First = b.First.Next; x.Next = a.First; a.First = x; } var current = a.First; while (b.First != null && current.Next != null) { if (b.First.Value < current.Next.Value) { x = b.First; b.First = b.First.Next; x.Next = current.Next; current.Next = x; } current = current.Next; } if (b.First != null)// this element should be at the end of a { current.Next = b.First; b.First = null; } Console.Write("List 1: "); a.Print(); Console.Write("List 2: "); b.Print(); return(a); }
static public void SwapPairwise(MyLinkedList <int> l) { Console.WriteLine("Original list: "); l.Print(); var a = l.First; if (l == null || a.Next == null) { return; } l.First = a.Next; //list with 2 elements if (a.Next.Next == null) { a.Next.Next = a; a.Next = null; return; } //for more than 2 elements. A and B in position var b = a.Next.Next; while (a.Next.Next != null) { a.Next.Next = a; if (b.Next == null) // odd number of elements { a.Next = b; break; } a.Next = b.Next; if (b.Next.Next == null)// even number of elements { b.Next.Next = b; b.Next = null; break; } a = b; b = a.Next.Next; } }
/// <summary> /// Given two numbers represented by two lists, write a function that returns sum list. /// The sum list is list representation of addition of two input numbers. /// Suppose you have two linked list 5->4 ( 4 5 )and 5->4->3( 3 4 5) you have to return a resultant linked list 0->9->3 (3 9 0). /// </summary> /// <param name="l1"></param> /// <param name="l2"></param> static public void SUM(MyLinkedList <int> l1, MyLinkedList <int> l2) { int carry = 0; int suma = 0; var current1 = l1.First; var current2 = l2.First; var previous1 = current1; var previous2 = current2; while (current1 != null && current2 != null) { suma = current1.Value + current2.Value; current2.Value = (suma % 10) + carry; carry = suma / 10; previous1 = current1; current1 = current1.Next; previous2 = current2; //if list one has fewer number of elem current2 = current2.Next; } if (current1 == null && current2 != null) { current2.Value += carry; } if (current1 != null && current2 == null) { current1.Value += carry; previous2.Next = current1; previous1.Next = null; } l1.First = null; l2.Print(); }