Rotate List LeetCode Solution

Problem – Rotate List

Given the head of a linked list, rotate the list to the right by k places.

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]


  • The number of nodes in the list is in the range [0, 500].
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109

Rotate List LeetCode Solution in C++

class Solution {
    ListNode* rotateRight(ListNode* head, int k) {
        if(!head) return head;
        int len=1; // number of nodes
        ListNode *newH, *tail;
        while(tail->next)  // get the number of nodes in the list
            tail = tail->next;
        tail->next = head; // circle the link

        if(k %= len) 
            for(auto i=0; i<len-k; i++) tail = tail->next; // the tail node is the (len-k)-th node (1st node is head)
        newH = tail->next; 
        tail->next = NULL;
        return newH;

Rotate List LeetCode Solution in Java

public ListNode rotateRight(ListNode head, int n) {
    if (head==null|| return head;
    ListNode dummy=new ListNode(0);;
    ListNode fast=dummy,slow=dummy;

    int i;
    for (i=0;!=null;i++)//Get the total length;
    for (int j=i-n%i;j>0;j--) //Get the i-n%i th node;; //Do the rotation;;

Rotate List LeetCode Solution in Python

class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if not head:
            return None
        lastElement = head
        length = 1
        # get the length of the list and the last node in the list
        while ( ):
            lastElement =
            length += 1

        # If k is equal to the length of the list then k == 0
        # ElIf k is greater than the length of the list then k = k % length
        k = k % length
        # Set the last node to point to head node
        # The list is now a circular linked list with last node pointing to first node = head
        # Traverse the list to get to the node just before the ( length - k )th node.
        # Example: In 1->2->3->4->5, and k = 2
        #          we need to get to the Node(3)
        tempNode = head
        for _ in range( length - k - 1 ):
            tempNode =
        # Get the next node from the tempNode and then set the as None
        # Example: In 1->2->3->4->5, and k = 2
        #          tempNode = Node(3)
        #          answer = Node(3).next => Node(4)
        #          Node(3).next = None ( cut the linked list from here )
        answer = = None
        return answer
Rotate List LeetCode Solution Review:

In our experience, we suggest you solve this Rotate List LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Rotate List LeetCode Solution

Find on LeetCode


I hope this Rotate List LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.