Reverse Linked List
Explore the process of reversing a singly linked list by manipulating it in-place. This lesson helps you understand how to update the linked list head efficiently while working within given constraints, preparing you for common coding interview challenges.
We'll cover the following...
Statement
Given the head of a singly linked list, reverse the linked list and return its updated head.
Constraints:
Let n be the number of nodes in a linked list.
-
n -
Node.value
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
What is the output if the following linked list is provided as input?
4 → 2 → 7 → 8 → 9 → 0 → 2
9 → 0 → 2 → 8 → 4 → 2 → 7
7 → 2 → 4 → 8 → 2 → 0 → 9
2 → 0 → 9 → 8 → 7 → 2 → 4
0 → 2 → 2 → 4 → 8 → 9
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
// Definition for a Linked List node// class ListNode {// constructor(val = 0, next = null) {// this.val = val;// this.next = next;// }// }import {ListNode} from './ds_v1/LinkedList.js';function reverse(head) {// Replace this placeholder return statement with your codereturn head;}export {reverse};