Reverse a Linked List
Updated
Problem statement
The argument head is the front node of a list whose nodes link forward only, or null for a list with zero nodes. Rearrange the links so that following them from the new front visits the values from last to first.
Return the node that was last in the input. With zero nodes there is nothing to rearrange, so a null input gives null.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [6,-2,9,4]return = [4,9,-2,6]The list starts as 6 -> -2 -> 9 -> 4. Once each of its three links points the other way, node 4 has no node in front of it, so it is the head. Reading from there gives 4 -> 9 -> -2 -> 6, and node 6 now links to null.
Example 2
head = [7,-3]return = [-3,7]With two nodes, the single link turns around. Node -3 now points to node 7, and 7 becomes the last node.
Example 3
head = []return = []The head passed in is already null. No pointer exists to flip, so that same null is the answer.
Constraints
- The number of nodes in the list is between
0and2000. -1000 <= Node.val <= 1000
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- 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
- Reverse a Singly Linked ListGoogle · ONSITE INTERVIEWGoogle 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
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences