Beispiel #1
0
        public void ReturnKToLastNodeTest1()
        {
            SinglyLinkedNodes <String> singlyLinkedNodes = new SinglyLinkedNodes <string>(words);
            int index  = 2;
            var result = ReturnKthToLastNode.Solution1(singlyLinkedNodes, index);

            Assert.AreEqual(result.Length, words.Length - index);
            for (int i = 0; i < words.Length - 2; i++)
            {
                Assert.AreEqual(words[i + index], result[i].Data);
            }
        }
        public static Node <T>[] Solution1 <T>(SinglyLinkedNodes <T> singlyLinkedNodes, int startIndex)
        {
            int length = singlyLinkedNodes.Length;

            Node <T>[] result = new Node <T> [length - startIndex];
            Node <T>   node = singlyLinkedNodes.Head;
            int        count = 0, index = 0;;

            while (node != null)
            {
                if (count >= startIndex)
                {
                    result[index] = node;
                    index++;
                }
                node = node.Next;
                count++;
            }
            return(result);
        }