Reverse Linked List

Easy

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

Examples

Input: head = [1,2,3,4,5]

Output: [5,4,3,2,1]

Input: head = [1,2]

Output: [2,1]

Input: head = []

Output: []

Constraints

  • Time complexity matters. Can you solve it in O(n) time?
  • Consider edge cases such as empty inputs or extreme values.
  • Optimize your solution for readability and maintainability.
// Write your solution here
function solution() {
// Your code here
}

Tips

  • • Consider using a hash map for efficient lookups
  • • Think about the time vs. space tradeoffs
  • • Can you solve it in a single pass?