Reverse a linked list
Reverse a singly linked list in place and return its new head.
Course progress
Problem 33 of 100
Example → approach → runnable solution
Worked example
See the problem in action
Trace a concrete input through to the verified result.
Example input
list = 1 -> 2 -> 3
Expected output
3 2 1
What is happening?
Redirect each next pointer toward the previous node. The old tail becomes the new head, producing 3 -> 2 -> 1.
Solution strategy
Build the right mental model
Walk through the list while redirecting each node's Next reference to the previous node. Save the original Next before changing it.
Core concepts
linked list · references
Complexity
Time O(n), space O(1).
Runnable solution
Learn from working code
Compare languages, copy the code, or open it in the compiler.
Complete C# solution for reverse-linked-list
using System;
using System.Collections.Generic;
class Program
{
class Node
{
public int Value { get; private set; }
public Node Next { get; set; }
public Node(int value, Node next = null)
{
Value = value;
Next = next;
}
}
static Node Reverse(Node current)
{
Node previous = null;
while (current != null)
{
Node next = current.Next;
current.Next = previous;
previous = current;
current = next;
}
return previous;
}
static void Main()
{
Node head = new Node(1, new Node(2, new Node(3)));
var values = new List<int>();
for (Node node = Reverse(head); node != null; node = node.Next) values.Add(node.Value);
Console.WriteLine(string.Join(" ", values));
}
}Knowledge check
Questions students often ask
What is the main idea behind reverse a linked list?
Walk through the list while redirecting each node's Next reference to the previous node. Save the original Next before changing it.
What is the time and space complexity of this solution?
Time O(n), space O(1).
Do all language tabs solve the same problem?
Yes. Every program uses the same example and produces the verified output shown on this page. The syntax and a few language-specific data structures differ.