Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

79. Rotate List

MediumDay 23 of 60Linked List

Given the head of a linked list and a non-negative integer k, rotate the list to the right by k places. One right rotation moves the last node to the front. Return the new head.

Example 1
Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
Example 2
Input: head = [0,1,2], k = 4
Output: [2,0,1]
Rotating 3 times gives the same list, so 4 rotations equal 1 rotation.
Constraints
  • 0 <= number of nodes <= 500
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 10^9
Hints
Frequently asked at: Microsoft, Amazon, LinkedIn, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min