有次看见别人的面试题问起链表反序.自己实验下.
Code
1 public class Node<T> : ICloneable
2 {
3 public T Data { get; set; }
4
5 public Node<T> Next { get; set; }
6 /**//// <summary>
7 /// 在遍历当前链表时,把遍历的当前节点赋给新的节点,新的节点会记录当前数据,加进新链表的前头,算法为O(N)次.
8 /// </summary>
9 /// <param name="node"></param>
10 /// <returns></returns>
11 public static Node<T> reverse(Node<T> node)
12 {
13 Node<T> head = null;
14 while (node != null)
15 {
16 Node<T> newNode = new Node<T>();
17 //设置新节点的数据为当前的节点的数据,并连接head
18 newNode.Data = node.Data;
19 newNode.Next = head;
20 //head记录头节点
21 head = newNode;
22
23 //遍历下一个节点
24 node = node.Next;
25 }
26 return head;
27 }
28 public object Clone()
29 {
30 Node<T> newnode = new Node<T>();
31 newnode.Data = this.Data;
32 return newnode;
33 }
34 }
1 public class Node<T> : ICloneable
2 {
3 public T Data { get; set; }
4
5 public Node<T> Next { get; set; }
6 /**//// <summary>
7 /// 在遍历当前链表时,把遍历的当前节点赋给新的节点,新的节点会记录当前数据,加进新链表的前头,算法为O(N)次.
8 /// </summary>
9 /// <param name="node"></param>
10 /// <returns></returns>
11 public static Node<T> reverse(Node<T> node)
12 {
13 Node<T> head = null;
14 while (node != null)
15 {
16 Node<T> newNode = new Node<T>();
17 //设置新节点的数据为当前的节点的数据,并连接head
18 newNode.Data = node.Data;
19 newNode.Next = head;
20 //head记录头节点
21 head = newNode;
22
23 //遍历下一个节点
24 node = node.Next;
25 }
26 return head;
27 }
28 public object Clone()
29 {
30 Node<T> newnode = new Node<T>();
31 newnode.Data = this.Data;
32 return newnode;
33 }
34 }
在主程序下:
Code
1 static void Test1()
2 {
3 Node<string> s6 = new Node<string> { Data="6"};
4 Node<string> s5 = new Node<string> { Data = "5", Next = s6 };
5 Node<string> s4 = new Node<string> { Data = "4", Next = s5 };
6 Node<string> s3 = new Node<string> { Data = "3", Next = s4 };
7 Node<string> s2 = new Node<string> { Data = "2", Next = s3 };
8 Node<string> s1 = new Node<string> { Data = "1", Next = s2 };
9
10 Node<string> head = Node<string>.reverse(s1);
11
12
13 while (head != null)
14 {
15 Console.Write(head.Data);
16 head = head.Next;
17 }
18 Console.Read();
19 }
1 static void Test1()
2 {
3 Node<string> s6 = new Node<string> { Data="6"};
4 Node<string> s5 = new Node<string> { Data = "5", Next = s6 };
5 Node<string> s4 = new Node<string> { Data = "4", Next = s5 };
6 Node<string> s3 = new Node<string> { Data = "3", Next = s4 };
7 Node<string> s2 = new Node<string> { Data = "2", Next = s3 };
8 Node<string> s1 = new Node<string> { Data = "1", Next = s2 };
9
10 Node<string> head = Node<string>.reverse(s1);
11
12
13 while (head != null)
14 {
15 Console.Write(head.Data);
16 head = head.Next;
17 }
18 Console.Read();
19 }
十进制与别的进制相互转化:
///
<summary>
/// 把相关十进制整数转成x进制
/// </summary>
/// <param name="i"> 十进制整数 </param>
/// <param name="x"> x进制 </param>
/// <returns></returns>
public int DOI( int i, int x)
{
int result = 0;
int j = 0;
while (i > 0)
{
result += ( int)Math.Pow( 10,j)*(i % x);
i = i / x;
j++;
}
return result;
}
/// <summary>
/// 把相关x进制整数转成十进制
/// </summary>
/// <param name="i"> x进制整数 </param>
/// <param name="x"> x进制 </param>
/// <returns></returns>
public int IOD( int i, int x)
{
int result = 0;
int j = 0;
while (i > 0)
{
result += ( int)Math.Pow(x, j) * (i % 10);
i = i / 10;
j++;
}
return result;
}
/// <summary>
/// 根据上面二段推出x进制的数转成y进制
/// </summary>
/// <param name="i"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int IOI( int i, int x, int y)
{
int result = 0;
int j = 0;
while (i > 0)
{
result += ( int)Math.Pow(x, j) * (i % y);
i = i / y;
j++;
}
return result;
/// 把相关十进制整数转成x进制
/// </summary>
/// <param name="i"> 十进制整数 </param>
/// <param name="x"> x进制 </param>
/// <returns></returns>
public int DOI( int i, int x)
{
int result = 0;
int j = 0;
while (i > 0)
{
result += ( int)Math.Pow( 10,j)*(i % x);
i = i / x;
j++;
}
return result;
}
/// <summary>
/// 把相关x进制整数转成十进制
/// </summary>
/// <param name="i"> x进制整数 </param>
/// <param name="x"> x进制 </param>
/// <returns></returns>
public int IOD( int i, int x)
{
int result = 0;
int j = 0;
while (i > 0)
{
result += ( int)Math.Pow(x, j) * (i % 10);
i = i / 10;
j++;
}
return result;
}
/// <summary>
/// 根据上面二段推出x进制的数转成y进制
/// </summary>
/// <param name="i"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int IOI( int i, int x, int y)
{
int result = 0;
int j = 0;
while (i > 0)
{
result += ( int)Math.Pow(x, j) * (i % y);
i = i / y;
j++;
}
return result;
}