anton1172
BAN USERpublic class ListNode<T>
{
private T data;
public T Data
{
get { return data; }
}
private ListNode<T> nextNode;
public ListNode<T> NextNode
{
get { return nextNode; }
set { nextNode = value; }
}
public ListNode(T data)
{
this.data = data;
}
public override string ToString()
{
return data.ToString();
}
}
public class LinkedList<T> where T : class
{
ListNode<T> head = null;
public void Add(T data)
{
ListNode<T> newHead = new ListNode<T>(data);
newHead.NextNode = head;
head = newHead;
}
public void Show()
{
ListNode<T> current = head;
while (current != null)
{
Console.Write(current.Data.ToString() + "; ");
current = current.NextNode;
}
Console.Write(Environment.NewLine);
}
private ListNode<T> PrivateSwapTwo(ListNode<T> current)
{
if ((current != null) && (current.NextNode != null))
{
var tmp1 = current.NextNode.NextNode;
var tmp2 = current.NextNode;
current.NextNode.NextNode = current;
current.NextNode = PrivateSwapTwo(tmp1);
return tmp2;
}
else
{
return current;
}
}
public void SwapTwo()
{
head = PrivateSwapTwo(head);
}
}
I would use hashtable to keep visited places. Where key is some presentation of offsets X and Y, e.g. key = "0_0" is start position and key = "1_0" is left step from start position.
- anton1172 August 24, 2010