using System;
class Node
{
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}
class SLList
{
private Node head = null;
public void AddLeft(int val)
{
Node newNode = new Node(val);
newNode.next = head;
head = newNode;
}
public void Reverse()
{
Node prev = null;
Node next = null;
Node curr = head;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
public void Print()
{
Node temp = head;
while (temp != null)
{
Console.Write(temp.data + " -> ");
temp = temp.next;
}
Console.WriteLine("null");
}
}
public class MainClass
{
public static void Main(string[] args)
{
SLList list = new SLList();
list.AddLeft(50);
list.AddLeft(40);
list.AddLeft(30);
list.AddLeft(20);
list.AddLeft(10);
list.Print();
list.Reverse();
list.Print();
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: