using System.Collections.Generic;
public sealed class ListNode<T>
public readonly ListNode<T> Next;
public ListNode<T> Previous;
public ListNode(T value,ListNode<T> next)
public class LinkedList<T>
private ListNode<T> _head;
private ListNode<T> _tail;
public LinkedList(IEnumerable<T> collection)
foreach (var t in collection.Reverse())
temp=new ListNode<T>(t,temp);
if (next != null) next.Previous = temp;
if(_tail==null) _tail = temp;
public LinkedList(T [] collection)
foreach (var t in collection.Reverse())
temp = new ListNode<T>(t, temp);
if (next != null) next.Previous = temp;
if (_tail == null) _tail = temp;
public void Replace(T search, T replace)
temp = temp.Value.Equals(search)
? new ListNode<T>(replace, next) { Previous = temp.Previous }
: new ListNode<T>(temp.Value, next){Previous = temp.Previous};
if (next == null) _tail = temp;
public static LinkedList<T> Join(LinkedList<T> first, LinkedList<T> last)
next=new ListNode<T>(temp.Value, next);
public void Foreach(Action<ListNode<T>> act)
public static void Main()
var linkedList=new LinkedList<string>( new []{"A","B","C","D","E","F"});
linkedList.Foreach(v=>{ Console.Write(string.Format("{0} ",v.Value));});
Console.WriteLine(string.Format("Replace {0}<=>{1}",seach,replace));
linkedList.Replace(seach,replace);
linkedList.Foreach(v => { Console.Write(string.Format("{0} ", v.Value)); });
var first = new LinkedList<string>(new[] { "1", "2", "3", "4", "5", "6" });
var second = new LinkedList<string>(new[] { "7", "8", "9", "10", "11", "12" });
Console.Write("First: ");
first.Foreach(v => { Console.Write(string.Format("{0} ", v.Value)); });
Console.Write("Second:: ");
second.Foreach(v => { Console.Write(string.Format("{0} ", v.Value)); });
var join = LinkedList<string>.Join(first, second);
Console.Write("Join:: ");
join.Foreach(v => { Console.Write(string.Format("{0} ", v.Value)); });