Reverse Linked List
Updated
Problem statement
head is the first node of a singly linked list. Every node stores a value and points to the node after it; the last node points to null. Reverse the list: the last node comes first, and each link is turned around, so every node points to the node that was before it. Return the new first node.
An empty list is allowed: then head is null, and the answer is null too.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [6,-4,15,0,9,2]return = [2,9,0,15,-4,6]Node 2 was last, so it comes first now. Every link is turned around: 9 now points to 0, 0 points to 15, and so on, and 6, the old first node, points to null. Reading from the new first node gives 2, 9, 0, 15, -4, 6.
Example 2
head = [2,2,7,2]return = [2,7,2,2]Three nodes hold the value 2. They are still three separate nodes, and all of them stay in the list. Reversed, the values read 2, 7, 2, 2.
Example 3
head = [-17]return = [-17]A list of one node is its own reverse: the node is both the first and the last node, and it still points to null.
Constraints
0 <= number of nodes <= 5000-5000 <= node.val <= 5000
More ByteDance Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Implement QuicksortPHONE SCREEN · Seen Dec 2026
- Largest Rectangle in HistogramPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Reverse Linked ListAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
- Reverse a Linked ListCisco · PHONE SCREENCisco interview questions
- Reverse a Linked ListApple · PHONE SCREENApple interview questions
- Reverse a Singly Linked ListChubb · PHONE SCREENChubb interview questions
- Reverse a Singly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Reverse a Singly Linked ListAdobe · PHONE SCREENAdobe interview questions
- Remove Linked List ElementsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Merge Two Sorted ListsWhatNot · ONSITE INTERVIEWWhatNot interview questions
- Remove Linked List Nodes with a Given ValueExpedia · ONSITE INTERVIEWExpedia interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience