Skip to main content

In-Place Reversal

This technique reverse the links between a set of nodes of a linked list using the existing node objects and without using extra memory.

How to Identify

  • Data Structure Involves: Linked List
  • Question Type: If the problem requires reversing a linked list without using extra memory.

Example

Reverse Linked List

Given the head of a singly linked list, reverse the list, and return the reversed list.

Example
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
reverseLinkedList.ts
import {ListNode} from "./ListNode"
function reverseList(head: ListNode | null): ListNode | null {
return reverse(null, head)
}
function reverse(previous: ListNode | null, current: ListNode | null): ListNode | null{
// base case
if(current == null) return previous

// recursive case
const next = current.next
current.next = previous

return reverse(current, next)
}
ProblemsDifficulty
1Reverse Linked ListEasy
2Swap Nodes in PairsMedium
3Reverse Nodes in k-GroupHard