easy Problem 1 Reverse Linked List
Problem Statement:
Given the head of a singly linked list, your task is to reverse the list and return its head. The singly linked list has nodes, and each node contains an integer and a pointer to the next node. The last node in the list points to null, indicating the end of the list.
Examples
Example 1:
- Input:
[3, 5, 2] - Expected Output:
[2, 5, 3] - Justification: Reversing the list
[3, 5, 2]gives us[2, 5, 3].
Example 2:
- Input:
[7] - Expected Output:
[7] - Justification: Since there is only one element in the list, the reversed list remains the same.
Example 3:
- Input:
[-1, 0, 1] - Expected Output:
[1, 0, -1] - Justification: The list is reversed, so the elements are in the order
[1, 0, -1].
Constraints:
- The number of nodes in the list is the range
[0, 5000]. -5000 <= Node.val <= 5000
Try it yourself
Try solving this question here:
✅ Solution Reverse Linked List
Problem Statement
Given the head of a singly linked list, return the head of the reversed list.
Examples
Example 1:
- Input:
[3, 5, 2] - Expected Output:
[2, 5, 3] - Justification: Reversing the list
[3, 5, 2]gives us[2, 5, 3].
Example 2:
- Input:
[7] - Expected Output:
[7] - Justification: Since there is only one element in the list, the reversed list remains the same.
Example 3:
- Input:
[-1, 0, 1] - Expected Output:
[1, 0, -1] - Justification: The list is reversed, so the elements are in the order
[1, 0, -1].
Constraints:
- The number of nodes in the list is the range
[0, 5000]. -5000 <= Node.val <= 5000
Solution
To reverse a singly linked list, begin by setting up two pointers: 'previous' as null and 'current' as the head of the list. Traverse through the list, and during each step, reverse the current node's next pointer to point to the previous node. Then, update 'previous' to be the current node, and 'current' to its original next node. Continue this process until you reach the end of the list. When 'current' becomes null, the reversal is complete, and the 'previous' pointer will be at the new head of the list.
- Initialization: Initialize three pointers:
prevtonull,currentto theheadof the list, andnexttonull. - Traversal and Reversal: Traverse the list. For each node, temporarily store the next node, update the current node's next pointer to point to the
prevnode, and move theprevandcurrentpointers one step forward. - Termination: When the
currentpointer reaches the end of the list (null), theprevpointer will be pointing at the new head of the reversed list.
This algorithm will work because, during the traversal, we are reversing the direction of the pointers between the nodes, which effectively reverses the list. It is efficient and uses only a constant amount of extra space.
Algorithm Walkthrough:
Given the input list [3, 5, 2]:
- Step 1: Initialize
prev = null,current = 3. - Step 2: Traverse the list.
- Store
next = 5. - Update
current.nexttoprev, so3now points tonull. - Move
prevto3andcurrentto5.
- Store
- Step 3: Repeat Step 2.
- Store
next = 2. - Update
current.nexttoprev, so5now points to3. - Move
prevto5andcurrentto2.
- Store
- Step 4: Repeat Step 2.
- Since there is no next node, set
next = null. - Update
current.nexttoprev, so2now points to5. - Move
prevto2,currentbecomesnull, and we exit the loop.
- Since there is no next node, set
- Step 5: The
previs now pointing to the head of the reversed list[2, 5, 3].
Here is the visual representation of the algorithm:
Code
Here is the code for this algorithm:
// class ListNode {
// int val;
// ListNode next;
// ListNode(int x) { val = x; }
// }
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode next = current.next; // store next node
current.next = prev; // reverse the link
prev = current; // move one step forward in the list
current = next; // move one step forward in the list
}
return prev; // prev is now pointing to the new head
}
public static void printList(ListNode head) {
while (head != null) {
System.out.print(head.val + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
Solution solution = new Solution();
// Test case 1
ListNode head1 = new ListNode(3);
head1.next = new ListNode(5);
head1.next.next = new ListNode(2);
printList(solution.reverseList(head1)); // Expected Output: 2 5 3
// Test case 2
ListNode head2 = new ListNode(7);
printList(solution.reverseList(head2)); // Expected Output: 7
// Test case 3
ListNode head3 = new ListNode(-1);
head3.next = new ListNode(0);
head3.next.next = new ListNode(1);
printList(solution.reverseList(head3)); // Expected Output: 1 0 -1
}
}
Complexity Analysis
Time Complexity
The time complexity of the reverse linked list algorithm is
Space Complexity
The space complexity of the algorithm is
🤖 Don't fully get this? Learn it with Claude
Stuck on Problem 1 Reverse Linked List? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Problem 1 Reverse Linked List** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Problem 1 Reverse Linked List** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Problem 1 Reverse Linked List**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Problem 1 Reverse Linked List**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.