C# Program to Reverse Linked List with Example
Next pointer to the previous node, like this: while (current != null) { var next = current.Next; current.Next = prev; prev = current; current = next; }.Examples
How to Think About It
Algorithm
Code
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(); } }
Dry Run
Let's trace reversing the list 1 -> 2 -> 3 -> null through the code
Initialize pointers
prev = null, current = Node(1), next = null
First iteration
next = Node(2), current.Next = prev (null), prev = Node(1), current = Node(2)
Second iteration
next = Node(3), current.Next = prev (Node(1)), prev = Node(2), current = Node(3)
Third iteration
next = null, current.Next = prev (Node(2)), prev = Node(3), current = null
End loop
current is null, set Head = prev (Node(3))
| prev | current | next | current.Next |
|---|---|---|---|
| null | Node(1) | Node(2) | null |
| Node(1) | Node(2) | Node(3) | null |
| Node(2) | Node(3) | null | Node(1) |
| Node(3) | null | null | Node(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
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; }
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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative in-place | O(n) | O(1) | Large lists, memory efficient |
| Recursive | O(n) | O(n) | Simple code, small lists |
| Stack-based | O(n) | O(n) | Easy to understand, uses extra memory |