using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { static void Main(string[] args) { Random rnd = new Random(123); LinkedList<int> list = new LinkedList<int>(); for (int i = 0; i < 25; i++) { list.AddLast(rnd.Next(0, 5)); } Console.WriteLine("До:"); foreach (var i in list) { Console.WriteLine(i); } Console.WriteLine(); LinkedListNode<int> node; var d = 0; do { d = 0; node = list.First; while (node != null) { int prev = -9999; int next = -99; if (node.Previous != null) { prev = node.Previous.Value; } if (node.Next != null) { next = node.Next.Value; } var nextNode = node.Next; if (node.Value == prev || prev == next || node.Value == next) { d++; Console.WriteLine("DELETE: Prev:{0} ,Curr:{1}, Next:{2}", prev, node.Value, next); //Удаление элемента list.Remove(node); } node = nextNode; } } while (d != 0); Console.WriteLine(); Console.WriteLine("После:"); foreach (var i in list) { Console.WriteLine(i); } Console.ReadKey(); } } }
Standard input is empty
До: 4 3 1 4 4 0 0 2 4 3 0 4 2 4 0 0 1 0 0 1 3 4 2 3 1 DELETE: Prev:1 ,Curr:4, Next:4 DELETE: Prev:4 ,Curr:0, Next:0 DELETE: Prev:4 ,Curr:2, Next:4 DELETE: Prev:4 ,Curr:4, Next:0 DELETE: Prev:4 ,Curr:0, Next:0 DELETE: Prev:0 ,Curr:1, Next:0 DELETE: Prev:0 ,Curr:0, Next:0 DELETE: Prev:0 ,Curr:0, Next:1 DELETE: Prev:0 ,Curr:4, Next:0 DELETE: Prev:0 ,Curr:0, Next:1 После: 4 3 1 4 0 2 4 3 0 1 3 4 2 3 1