SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Reverse Linked List

Reverse every next pointer in a singly linked list.

Try It Yourself

Use the editor below to reverse a singly linked list.

Reverse the existing nodes

Change the pointers without creating replacement nodes. Preserve the next node before changing the current node's pointer. Handle empty and single-node lists.

Submit your implementation when it passes the examples.

Reverse Linked List

easyLinked List · Pointers25 minLC 206
Problem

Given the head of a singly linked list, reverse the direction of every next pointer.

Return the new head. Each node has the shape { val, next }.

Example 1:

Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: The last node, 5, becomes the new head. Following the reversed links visits 5, 4, 3, 2, and then 1.

Example 2:

Input: head = [1, 2]
Output: [2, 1]
Explanation: Reversing the link makes 2 the new head and 1 the last node.

Constraints

  • 0 ≤ number of nodes ≤ 5,000
  • Do not create replacement nodes.
0 attempts

Test results

Results appear here after a run.

Solution

The current node 2 redirects its next link to 1 while a saved reference to 3 preserves the rest of the list.

previous heads the part already reversed. current heads the part still to visit. On 1 → 2 → 3, save the link to 2 before pointing 1.next to null. Now previous is 1 and current is 2, so both pieces remain reachable. Repeat the same four assignments for each remaining node.

When current reaches null, previous points to the original tail, which is now the new head.

function reverseList(head) {
  let previous = null;
  let current = head;

  while (current !== null) {
    const next = current.next;
    current.next = previous;
    previous = current;
    current = next;
  }

  return previous;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Every node is visited and rewired exactly once.
Auxiliary spaceO(1)The reversal uses three node references regardless of list length.

How is this practice?

Previous lessonSingly Linked ListNext lessonDoubly Linked List
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation