Reverse a Sublist of a Linked List
Updated
Problem statement
You are given the head of a singly linked list and two integers, start and length. Positions in the list are numbered from 1, so the head is at position 1.
Reverse the length nodes that begin at position start. All other nodes keep their order. Return the head of the changed list.
The list is shown in the examples as the values of its nodes from head to tail.
Function
reverseSublist(head: ListNode, start: int, length: int) → ListNodeExamples
Example 1
head = [1,2,3,4,5]start = 2length = 3return = [1,4,3,2,5]Moved or new
The part to reverse starts at position 2 and has 3 nodes: 2 -> 3 -> 4. Reversed, it becomes 4 -> 3 -> 2. The nodes 1 and 5 stay where they are, so the list is 1 -> 4 -> 3 -> 2 -> 5.
Example 2
head = [5]start = 1length = 1return = [5]The part to reverse is the single node 5. Reversing one node changes nothing.
Example 3
head = [3,-1,7,0]start = 1length = 4return = [0,7,-1,3]Moved or new
The part starts at the head and covers all 4 nodes, so the whole list is reversed: 0 -> 7 -> -1 -> 3.
Constraints
- The number of nodes in the list is
n, where1 <= n <= 500. -500 <= Node.val <= 5001 <= start <= n1 <= length <= n - start + 1
More Salesforce Onsite Interview questions
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
- Flatten Nested Map Paths with a StackONSITE INTERVIEW · Seen Sep 2026
- LFU CacheONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Element in an ArrayONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- LRU CacheLotusFlare · ONSITE INTERVIEW
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Remove Nth Node From End of ListDruva · ONSITE INTERVIEW
- Merge a List In Between Two PositionsWalmart · ONSITE INTERVIEWWalmart interview questions
- LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LRU Cache OperationsHevo Data · ONSITE INTERVIEW
- LRU Cache OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- First-Fit Memory AllocatorGoogle · ONSITE INTERVIEWGoogle interview questions
- Find the Entry of a Linked List CycleQualys · ONSITE INTERVIEWQualys interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions