0
0
CsharpProgramBeginner · 2 min read

C# Program to Reverse Linked List with Example

To reverse a linked list in C#, use a loop to change each node's Next pointer to the previous node, like this: while (current != null) { var next = current.Next; current.Next = prev; prev = current; current = next; }.
📋

Examples

Input1 -> 2 -> 3 -> null
Output3 -> 2 -> 1 -> null
Input10 -> 20 -> null
Output20 -> 10 -> null
Inputnull
Outputnull
🧠

How to Think About It

To reverse a linked list, think of walking through the list from start to end, and for each node, flip its pointer to point backward instead of forward. You keep track of the previous node and the next node as you go, so you don't lose the rest of the list. At the end, the last node you visited becomes the new head.
📐

Algorithm

1
Start with three pointers: previous as null, current as head, and next as null.
2
While current is not null, do the following:
3
Save current's next node in next.
4
Change current's next pointer to previous.
5
Move previous to current.
6
Move current to next.
7
After the loop, previous will be the new head of the reversed list.
💻

Code

csharp
using System;

class Node {
    public int Data;
    public Node Next;
    public Node(int data) { Data = data; Next = null; }
}

class LinkedList {
    public Node Head;

    public void Add(int data) {
        Node newNode = new Node(data);
        newNode.Next = Head;
        Head = newNode;
    }

    public void Reverse() {
        Node prev = null;
        Node current = Head;
        while (current != null) {
            Node next = current.Next;
            current.Next = prev;
            prev = current;
            current = next;
        }
        Head = prev;
    }

    public void Print() {
        Node temp = Head;
        while (temp != null) {
            Console.Write(temp.Data + " -> ");
            temp = temp.Next;
        }
        Console.WriteLine("null");
    }
}

class Program {
    static void Main() {
        LinkedList list = new LinkedList();
        list.Add(3);
        list.Add(2);
        list.Add(1);
        Console.WriteLine("Original list:");
        list.Print();
        list.Reverse();
        Console.WriteLine("Reversed list:");
        list.Print();
    }
}
Output
Original list: 1 -> 2 -> 3 -> null Reversed list: 3 -> 2 -> 1 -> null
🔍

Dry Run

Let's trace reversing the list 1 -> 2 -> 3 -> null through the code

1

Initialize pointers

prev = null, current = Node(1), next = null

2

First iteration

next = Node(2), current.Next = prev (null), prev = Node(1), current = Node(2)

3

Second iteration

next = Node(3), current.Next = prev (Node(1)), prev = Node(2), current = Node(3)

4

Third iteration

next = null, current.Next = prev (Node(2)), prev = Node(3), current = null

5

End loop

current is null, set Head = prev (Node(3))

prevcurrentnextcurrent.Next
nullNode(1)Node(2)null
Node(1)Node(2)Node(3)null
Node(2)Node(3)nullNode(1)
Node(3)nullnullNode(2)
💡

Why This Works

Step 1: Track previous and current nodes

We keep a prev pointer to remember the reversed part and a current pointer to process nodes one by one.

Step 2: Reverse the link

For each node, we set its Next to prev, flipping the direction.

Step 3: Move forward

We move prev and current forward to continue reversing until the list ends.

Step 4: Update head

At the end, prev points to the new head of the reversed list.

🔄

Alternative Approaches

Recursive reversal
csharp
public Node ReverseRecursive(Node head) {
    if (head == null || head.Next == null) return head;
    Node newHead = ReverseRecursive(head.Next);
    head.Next.Next = head;
    head.Next = null;
    return newHead;
}
Uses recursion to reverse, simpler code but uses call stack, which may cause stack overflow on very long lists.
Using a stack
csharp
public void ReverseWithStack() {
    Stack<Node> stack = new Stack<Node>();
    Node temp = Head;
    while (temp != null) {
        stack.Push(temp);
        temp = temp.Next;
    }
    if (stack.Count == 0) return;
    Head = stack.Pop();
    temp = Head;
    while (stack.Count > 0) {
        temp.Next = stack.Pop();
        temp = temp.Next;
    }
    temp.Next = null;
}
Uses extra memory for stack, easier to understand but less efficient in space.

Complexity: O(n) time, O(1) space

Time Complexity

The algorithm visits each node once in a single loop, so it runs in linear time O(n), where n is the number of nodes.

Space Complexity

It uses only a few pointers and reverses the list in place, so space complexity is O(1).

Which Approach is Fastest?

The iterative in-place reversal is fastest and most memory efficient compared to recursive or stack-based methods.

ApproachTimeSpaceBest For
Iterative in-placeO(n)O(1)Large lists, memory efficient
RecursiveO(n)O(n)Simple code, small lists
Stack-basedO(n)O(n)Easy to understand, uses extra memory
💡
Always keep track of the next node before changing pointers to avoid losing the rest of the list.
⚠️
Forgetting to update the head pointer after reversing leaves the list unchanged.