Rotate a List Right by a Large Count

Quick Overview

Rotate a singly linked list right by a possibly large count, retaining its pointer interface and cyclic position semantics.

Rotate a List Right by a Large Count

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Rotate a singly linked list to the right by `k` positions and return its resulting head. One right rotation moves the last node to the front. Preserve the list order induced by those rotations. The source uses `ListNode`, with `int val` and `ListNode* next` fields: ```cpp ListNode* rotateRight(ListNode* head, int k); ``` Square brackets in the examples display node values along the chain; they do not replace the pointer interface. ### Examples ```text Input: head = [9,2,7,4], k = 6 Output list: [7,4,9,2] ``` Six right rotations have the same effect as two for this four-node list. ```text Input: head = [5,8], k = 0 Output list: [5,8] ``` ### Constraints - The list contains between 0 and 500 nodes. - Node values are between -100 and 100, inclusive. - `0 <= k <= 2000000000`. - Return a list head, not a flattened value array.

Overview: Rotate a singly linked list right by a possibly large count, retaining its pointer interface and cyclic position semantics.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Rotate a singly linked list to the right by k positions and return its resulting head. One right rotation moves the last node to the front. Preserve the list order induced by those rotations.

The source uses ListNode, with int val and ListNode* next fields:

ListNode* rotateRight(ListNode* head, int k);

Square brackets in the examples display node values along the chain; they do not replace the pointer interface.

Examples

Input: head = [9,2,7,4], k = 6
Output list: [7,4,9,2]

Six right rotations have the same effect as two for this four-node list.

Input: head = [5,8], k = 0
Output list: [5,8]

Constraints

  • The list contains between 0 and 500 nodes.
  • Node values are between -100 and 100, inclusive.
  • 0 <= k <= 2000000000 .
  • Return a list head, not a flattened value array.