[面试题]单向链表的倒序索引值?

本文介绍了一种在单向链表中通过倒序索引获取指定节点的方法。该方法通过两个指针同步遍历的方式实现,适用于不改变原链表结构的情况下查找倒数第N个节点。
 1 None.gif class  Program
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        static void Main(string[] args)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 5InBlock.gif            myNode firstNode = new myNode();
 6InBlock.gif            firstNode.Value = 0;
 7InBlock.gif
 8InBlock.gif            //创建一个单向链表
 9InBlock.gif            myNode tempNode = firstNode;
10InBlock.gif            for (int i = 1; i < 100; i++)
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                myNode currentNode = new myNode();
13InBlock.gif                currentNode.Value = i;
14InBlock.gif                tempNode.Next = currentNode;
15InBlock.gif
16InBlock.gif                tempNode = currentNode;
17ExpandedSubBlockEnd.gif            }

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

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

45InBlock.gif
46InBlock.gif            if (Node2 == nullreturn null;
47InBlock.gif
48InBlock.gif            while ((Node2 = Node2.Next) != null)
49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
50InBlock.gif                Node1 = Node1.Next;
51ExpandedSubBlockEnd.gif            }

52InBlock.gif
53InBlock.gif            return Node1;
54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56InBlock.gif
57InBlock.gif        class myNode
58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
59InBlock.gif            private int _Value;
60InBlock.gif            private myNode _next;
61InBlock.gif
62InBlock.gif            public myNode Next
63ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
64ExpandedSubBlockStart.gifContractedSubBlock.gif                get dot.gifreturn _next; }
65ExpandedSubBlockStart.gifContractedSubBlock.gif                set dot.gif{ _next = value; }
66ExpandedSubBlockEnd.gif            }

67InBlock.gif
68InBlock.gif            public int Value
69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
70ExpandedSubBlockStart.gifContractedSubBlock.gif                get dot.gifreturn _Value; }
71ExpandedSubBlockStart.gifContractedSubBlock.gif                set dot.gif{ _Value = value; }
72ExpandedSubBlockEnd.gif            }

73ExpandedSubBlockEnd.gif        }

74ExpandedBlockEnd.gif    }
posted on 2007-04-16 07:04 K3 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/sskset/archive/2007/04/16/714759.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值