Reverse a Linked List
Updated
Problem statement
You are given head, the first node of a singly linked list. Turn it around: every node's next pointer should lead to the node that came before it, so the old last node becomes the first.
Return the head of the reversed list. An empty list is given as null, and its reverse is also null.
Function
reverseList(head: ListNode) → ListNodeExamples
Example 1
head = [12,-5,30,7,0]return = [0,7,30,-5,12]Moved or new
The nodes hold 12, -5, 30, 7, 0 from head to tail. After the reversal, the old tail 0 is the head and the old head 12 is the tail, whose next is null.
Example 2
head = [-417]return = [-417]A list with one node reads the same in both directions, so that node is returned.
Example 3
head = [-8,-8,3]return = [3,-8,-8]Moved or new
The two nodes with value -8 are different nodes. Both move behind 3, which is now the head.
Constraints
- The list has
0to2000nodes. -10^4 <= Node.val <= 10^4
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
Similar questions at other companies
- Reverse a Singly Linked ListMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Merge Two Sorted ListsWhatNot · ONSITE INTERVIEWWhatNot interview questions
- Remove Linked List Nodes with a Given ValueExpedia · ONSITE INTERVIEWExpedia interview questions
- Sorted Doubly Linked List to Balanced BST In PlaceSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Reverse Nodes in K-GroupAmazon · ONSITE INTERVIEW
- Merge K Sorted Linked ListsThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Merge K Sorted ListsSkydio · ONSITE INTERVIEWSkydio interview questions
- Merge K Sorted Linked Lists Into OneCapital One · ONSITE INTERVIEWCapital One interview questions
- Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions