FastPrepReverse a Sublist of a Linked List
Problems/Reverse a Sublist of a Linked List

Reverse a Sublist of a Linked List

Salesforce logoSalesforce● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the first technical round of a Salesforce MTS loop in Hyderabad, after questions on distributed systems, the candidate got two coding questions. One was to reverse part of a linked list, given the head, the position where the part begins and how many nodes it has.

The post gives no examples or limits. Here positions start at 1, the part always fits inside the list, and the list has up to 500 nodes valued from -500 to 500.

Our version is about 90% the same as the reported question.

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) → ListNode

Examples

Example 1

head = [1,2,3,4,5]start = 2length = 3return = [1,4,3,2,5]
head
12345
→
return
14325
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]
head
5
→
return
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]
head
3-170
→
return
07-13
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, where 1 <= n <= 500.
  • -500 <= Node.val <= 500
  • 1 <= start <= n
  • 1 <= 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

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseSublist(ListNode head, int start, int length) {
        // Write your code here.
    }
}
Checking account…