using System.Diagnostics;
public static void Main()
var head = new Node("aba", new Node("b", new Node("cb")));
bool found = FindNeedle(new Node("ababbc"), "abc");
Console.WriteLine(found);
static bool FindNeedle(Node head, string niddle)
while (currentNode != null)
var nodeValue = currentNode.Value;
for (int i = 0; i < nodeValue.Length; i++)
Console.WriteLine("PRE:" + i + ":" + matchingIndex);
if (nodeValue[i] != niddle[matchingIndex])
Console.WriteLine("POST:" + i + ":" + matchingIndex);
if (matchingIndex == niddle.Length)
currentNode = currentNode.Next;
static void WhenHeadContainsNeedle_ReturnsTrue()
var head = new Node("abcd");
bool found = FindNeedle(head, "abcd");
found = FindNeedle(head, "bc");
static void WhenAnyNodeContainsNeedle_ReturnsTrue()
var node2 = new Node("bb");
var node1 = new Node("abcd", node2);
var head = new Node("add", node1);
bool found = FindNeedle(head, "bc");
node1 = new Node("abcd", node2);
head = new Node("add", node1);
found = FindNeedle(head, "da");
static void WhenNeedleSpreadsAcrossNodes_ReturnsTrue()
var node2 = new Node("c");
var node1 = new Node("b", node2);
var head = new Node("a", node1);
bool found = FindNeedle(head, "abc");
node1 = new Node("b", node2);
head = new Node("aa", node1);
found = FindNeedle(head, "abc");
static void WhenNeedleNotPartOfAnyNode_ReturnsFalse()
var node2 = new Node("c");
var node1 = new Node("b", node2);
var head = new Node("aabh", node1);
bool found = FindNeedle(head, "abc");
node1 = new Node("b", node2);
head = new Node("aabh", node1);
found = FindNeedle(head, "abc");
public Node(string value, Node? next = null)