{
this.Next = next;
this.Data = data;
}
}
其中,我们需要人为地在单链表前面加一个空节点,称其为
head
。例如,一个
单链表是
1->2->5
,如图所示:
对一个单链表的遍历如下所示:
static void Main(string[] args)
{
Link head = GenerateLink();
Link curr = head;
while (curr != null)
{
Console.WriteLine(curr.Data);
curr = curr.Next;
}
}
1.
单链表反转
这道题目有两种算法,既然是要反转,那么肯定是要破坏原有的数据结构的:
算法
1
:我们需要额外的两个变量来存储当前节点
curr
的下一个节点
next
、再
下一个节点
nextnext
:
public static Link ReverseLink1(Link head)
{
Link curr = head.Next;
Link next = null;
Link nextnext = null;
评论