Reverse a Linked List
Updated
Problem statement
Given the head of a singly linked list, reverse its links and return the new head.
Reuse the existing nodes. Each node must point to the node that preceded it in the original list; the original head becomes the tail and points to null.
The input is acyclic. An empty list remains empty. Lists are displayed as arrays of their node values.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [2,5,8]return = [8,5,2]Moved or new
The original last node becomes the head. The node order changes from 2 → 5 → 8 to 8 → 5 → 2.
Example 2
head = [4,4,-1]return = [-1,4,4]Moved or new
Reversal preserves all three nodes, including both nodes with value 4.
Constraints
- The list has
0to1000nodes. - Each node value is an integer in
[-10^6, 10^6]. - Node values may repeat.
- The input contains no cycle.
More Mygate Phone Screen questions
- Employees Earning More Than Their ManagerPHONE SCREEN · Seen Sep 2026
- Maximum Subarray SumPHONE SCREEN · Seen Sep 2026
- Users Active in Both January and FebruaryPHONE SCREEN · Seen Sep 2026
- Elements Occurring More Than n/3 TimesPHONE SCREEN · Seen Jan 2025
- Frog Jump: Minimum EnergyPHONE SCREEN · Seen Jan 2025
- Left View of a Binary TreePHONE SCREEN · Seen Jan 2025
- Two SumPHONE SCREEN · Seen Aug 2024
- Minimum in a Rotated Sorted ArrayPHONE SCREEN · Seen Jul 2024
- Minimum Subarray SumPHONE SCREEN · Seen Sep 2023
- Remove the Nth Node from the EndPHONE SCREEN · Seen Sep 2023
Similar questions at other companies
- Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
- Middle of a Linked ListCisco · PHONE SCREENCisco interview questions
- Merge Two Sorted ListsIBM · PHONE SCREENIBM interview questions
- Count Nodes in a ChainApple · PHONE SCREENApple interview questions
- Detect a Cycle in a Linked ListSalesforce · PHONE SCREENSalesforce interview questions
- Find the Intersection Node of Two Linked ListsVisa · PHONE SCREENVisa interview questions
- Linked List Palindrome in Constant SpaceNVIDIA · PHONE SCREENNVIDIA interview questions
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Merge Two Sorted Linked ListsExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions