Reverse a Singly Linked List
Updated
Problem statement
You are given the head head of a finite singly linked list of contacts. Reverse every next pointer in place and return the new head.
The returned list must contain exactly the original nodes and values in reverse order. Node values may repeat. If head is null, return null; if the list contains one node, return that same node.
Interview extensions
- Explain how to reverse the complete list recursively.
- Explain how to reverse only a contiguous segment from the
m-th node through then-th node.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [1,2,3,4,5]return = [5,4,3,2,1]Moved or new
Every pointer is reversed, so the original tail becomes the new head.
Example 2
head = []return = []An empty list has no nodes to reverse, so the result is null.
Example 3
head = [7]return = [7]A one-node list is already reversed, so the same node is returned.
Example 4
head = [1,1,2]return = [2,1,1]Moved or new
Repeated values are preserved because the nodes, rather than a set of values, are reversed.
Constraints
0 <= number of nodes <= 100000-10^9 <= node.val <= 10^9- The input list is finite and acyclic.
- Each node has only a
nextpointer; there is noprevpointer.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Reverse Linked ListAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Delete a Doubly Linked List Node at a PositionQualys · ONSITE INTERVIEWQualys interview questions
- Insert After a Linked-List NodeAMD · ONSITE INTERVIEWAMD interview questions
- Process Singly Linked List OperationsSEDEMAC · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Reverse a Singly Linked ListGEP · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions