public static void Main()
List<string> newList = new List<string>();
Console.WriteLine("This list has: " + newList.getListCount().ToString()+" elements:");
Console.WriteLine("Replacing element 3: D on C");
newList.replaceNode(3, "C");
Console.WriteLine("This list has: " + newList.getListCount().ToString() + " elements:");
Console.WriteLine("Creating new list");
List<string> newList2 = new List<string>();
Console.WriteLine("This list has: " + newList2.getListCount().ToString() + " elements:");
Console.WriteLine("Merging two lists");
newList.mergeTwoLists(newList2);
Console.WriteLine("This list has: " + newList.getListCount().ToString() + " elements:");
Console.WriteLine("Replacing last element: H on G");
newList.replaceNode(7, "G");
Console.WriteLine("This list has: " + newList.getListCount().ToString() + " elements:");
public readonly ListNode<T> Next;
public ListNode(T value, ListNode<T> next)
private ListNode<T> head;
public void addNode(T value)
head = new ListNode<T>(value, null);
ListNode<T> nextNode = new ListNode<T>(value, null);
head = new ListNode<T>(head.Value, nextNode);
ListNode<T> nextNodeHead = head;
bool final = false, firstHead = false;
head = new ListNode<T>(value, head);
nextNodeHead = head.Next.Next;
if (nextNodeHead == null)
ListNode<T> buf = new ListNode<T>(head.Value, null);
head = new ListNode<T>(head.Next.Value, buf);
head = new ListNode<T>(nextNodeHead.Value, head);
nextNodeHead = nextNodeHead.Next;
if (nextNodeHead == null)
ListNode<T> buf = new ListNode<T>(head.Value, null);
nextNodeHead = head.Next.Next;
head = new ListNode<T>(head.Next.Value, buf);
ListNode<T> buf = nextNodeHead;
nextNodeHead = nextNodeHead.Next;
head = new ListNode<T>(buf.Value, head);
if (nextNodeHead == null) break;
public void replaceNode(int index, T value)
if (index <= this.getListCount())
ListNode<T> nextNodeHead = head;
for (int i = 1; i < index; i++)
nextNodeHead = head.Next.Next;
ListNode<T> buf = new ListNode<T>(head.Value, null);
head = new ListNode<T>(head.Next.Value, buf);
head = new ListNode<T>(nextNodeHead.Value, head);
nextNodeHead = nextNodeHead.Next;
head = new ListNode<T>(value, head.Next);
ListNode<T> prevNode = nextNodeHead;
nextNodeHead = head.Next;
head = new ListNode<T>(value, prevNode);
head = new ListNode<T>(nextNodeHead.Value, head);
if (nextNodeHead.Next == null)
nextNodeHead = nextNodeHead.Next;
public int getListCount ()
public void mergeTwoLists (List<T> secondList)
ListNode<T> step = secondList.head;
this.addNode(step.Value);
T nodeValue = step.Value;
Console.WriteLine(nodeValue);