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 == null) return 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 }
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 == null) return 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 }