public static LinkedListsGetNthNode Push(LinkedListsGetNthNode head, int data) { LinkedListsGetNthNode node = new LinkedListsGetNthNode(data); node.Next = head; return(node); }
public static LinkedListsGetNthNode BuildOneTwoThree() { LinkedListsGetNthNode head = null; head = LinkedListsGetNthNode.Push(head, 3); head = LinkedListsGetNthNode.Push(head, 2); head = LinkedListsGetNthNode.Push(head, 1); return(head); }
// Override which allows user to "print" node and view data values // Example: // new Node(3, new Node(2, new Node(1, null))) -> "3 -> 2 -> 1 -> null" public override string ToString() { List <int> result = new List <int>(); LinkedListsGetNthNode curr = this; while (curr != null) { result.Add(curr.Data); curr = curr.Next; } return(String.Join(" -> ", result) + " -> null"); }
// Constructor for IEnumerable<int>s // Makes it easier to construct large lists public static LinkedListsGetNthNode Build(IEnumerable <int> values) { if (values == null || !values.Any()) { throw new ArgumentException("Node(IEnumerable<int>)'s first argument must not be empty."); } LinkedListsGetNthNode node = null; foreach (int value in Enumerable.Reverse(values)) { LinkedListsGetNthNode temp = node; node = new LinkedListsGetNthNode(value); node.Next = temp; } return(node); }
//LinkedListsGetNthNode -> Node, rename LinkedListsGetNthNode to Node in kata public static LinkedListsGetNthNode GetNth(LinkedListsGetNthNode node, int index) { return(node == null ? throw new ArgumentException() : index == 0 ? node : GetNth(node.Next, index - 1)); }
public LinkedListsGetNthNode(int data) { Data = data; Next = null; }