zoukankan      html  css  js  c++  java
  • [面试题]单向链表的倒序索引值?

     1class Program
     2    {
     3        static void Main(string[] args)
     4        {
     5            myNode firstNode = new myNode();
     6            firstNode.Value = 0;
     7
     8            //创建一个单向链表
     9            myNode tempNode = firstNode;
    10            for (int i = 1; i < 100; i++)
    11            {
    12                myNode currentNode = new myNode();
    13                currentNode.Value = i;
    14                tempNode.Next = currentNode;
    15
    16                tempNode = currentNode;
    17            }

    18
    19            //while ((tempNode = firstNode.Next) != null)
    20            //{
    21            //    Console.WriteLine(firstNode.Value);
    22            //    firstNode = firstNode.Next;
    23            //}
    24            //Console.WriteLine(firstNode.Value);
    25
    26
    27            myNode dd = GetNodeByLastIndex(34, firstNode);
    28
    29            Console.WriteLine("Result is {0}",dd.Value);
    30
    31            Console.ReadKey();
    32        }

    33
    34        static myNode GetNodeByLastIndex(int LastIndex, myNode topNode)
    35        {
    36            myNode Node1 = topNode;
    37            myNode Node2 = topNode;
    38
    39            int index = 0;
    40
    41            while (index < LastIndex && (Node2 = Node2.Next) != null)
    42            {
    43                index++;
    44            }

    45
    46            if (Node2 == nullreturn null;
    47
    48            while ((Node2 = Node2.Next) != null)
    49            {
    50                Node1 = Node1.Next;
    51            }

    52
    53            return Node1;
    54        }

    55
    56
    57        class myNode
    58        {
    59            private int _Value;
    60            private myNode _next;
    61
    62            public myNode Next
    63            {
    64                get return _next; }
    65                set { _next = value; }
    66            }

    67
    68            public int Value
    69            {
    70                get return _Value; }
    71                set { _Value = value; }
    72            }

    73        }

    74    }
  • 相关阅读:
    小波变换的引入,通俗易懂
    Leetcode 437. Path Sum III
    Leetcode 113. Path Sum II
    Leetcode 112 Path Sum
    Leetcode 520 Detect Capital
    Leetcode 443 String Compression
    Leetcode 38 Count and Say
    python中的生成器(generator)总结
    python的random模块及加权随机算法的python实现
    leetcode 24. Swap Nodes in Pairs(链表)
  • 原文地址:https://www.cnblogs.com/sskset/p/714759.html
Copyright © 2011-2022 走看看