我正在尝试反转一个链表。这是我想到的代码:
public static void Reverse(ref Node root)
{
Node tmp = root;
Node nroot = null;
Node prev = null;
while (tmp != null)
{
//Make a new node and copy tmp
nroot = new Node();
nroot.data = tmp.data;
nroot.next = prev;
prev = nroot;
tmp = tmp.next;
}
root = nroot;
}
它能够很好地工作。想知道是否可能避免创建新节点。希望得到相关建议。
System.Collections
命名空间中的选项都不能满足你的需求吗? - M.Babcock