-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
35 lines (34 loc) · 1017 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
namespace PrintImmutableLinkedListInReverse
{
class ImmutableListNode
{
public void PrintValue(); // print the value of this node.
public ImmutableListNode GetNext(); // return the next node.
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
public void PrintLinkedListInReverse_Recursion(ImmutableListNode head)
{
if (head == null) return;
PrintLinkedListInReverse_Recursion(head.GetNext());
head.PrintValue();
}
public void PrintLinkedListInReverse_List(ImmutableListNode head)
{
var list = new List<ImmutableListNode>();
while (head != null)
{
list.Add(head);
head = head.GetNext();
}
for (int i = list.Count - 1; i >= 0; i--)
list[i].PrintValue();
}
}
}