3. What will be the output of the following C# code after reversing the doubly linked list?
class Node {
public int data;
public Node next, prev;
public Node(int d) { data = d; next = prev = null; }
}
// List: 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second; second.prev = head;
second.next = third; third.prev = second;
// Reverse function
Node Reverse(Node head) {
Node current = head;
Node temp = null;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) head = temp.prev;
return head;
}
head = Reverse(head);
// Print list forward
Node tempNode = head;
string result = "";
while (tempNode != null) {
result += tempNode.data + " -> ";
tempNode = tempNode.next;
}
result += "null";
Console.WriteLine(result);