Rotate a Linked List (original) (raw)

Last Updated : 29 Aug, 2025

Given the **head of a singly linked list and an integer k, rotate the list to the left by k positions and return the updated head.

**Examples:

**Input: k= 4

1

**Output: 50 -> 10 -> 20 -> 30 -> 40
**Explanation: After rotating the linked list to the left by 4 places, the 5th node, i.e node 50 becomes the head of the linked list and next pointer of node 50 points to node 10.

2

**Input: k= 6

3

**Output: 30 -> 40 -> 10 -> 20
**Explanation: After rotating the list left by 6 positions, the node with value 30 becomes the new head, and the node with value 40 points to the node with value 10.

4

Try It Yourselfredirect icon

Table of Content

[Naive Approach] Shifting head node to the end k times - O(n × k) Time and O(1) Space

To rotate a linked list to the left k places, we can repeatedly move the head node to the end of the linked list k times.

C++ `

#include using namespace std;

class Node { public: int data; Node *next; Node(int new_data) { data = new_data; next = nullptr; } };

Node *rotate(Node *head, int k) { if (k == 0 || head == nullptr) return head;

// rotate the list by k nodes
for (int i = 0; i < k; ++i) {
    Node *curr = head;
    while (curr->next != nullptr)
        curr = curr->next;
    
    // move the first node to the last
    curr->next = head;
    curr = curr->next;
    head = head->next;
    curr->next = nullptr;
}
return head;

}

void printList(Node *node) { while (node != nullptr) { cout << node->data; if(node->next != NULL){ cout << " -> "; } node = node->next; } cout << endl; }

int main() {

Node *head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);

head = rotate(head, 6);
printList(head);

return 0;

}

C

#include <stdio.h> #include <stdlib.h>

struct Node { int data; struct Node* next; };

struct Node* createNode(int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; return new_node; }

struct Node* rotate(struct Node *head, int k) { if (k == 0 || head == NULL) return head;

// rotate the list by k nodes
for (int i = 0; i < k; ++i) {
    struct Node *curr = head;
    while (curr->next != NULL)
        curr = curr->next;

    // move the first node to the last
    curr->next = head;
    curr = curr->next;
    head = head->next;
    curr->next = NULL;
}
return head;

}

void printList(struct Node *node) { while (node != NULL) { printf("%d", node->data); if(node->next != NULL){ printf(" -> "); } node = node->next; } printf("\n"); }

int main() {

struct Node *head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
head->next->next->next = createNode(40);

head = rotate(head, 6);
printList(head);

return 0;

}

Java

class Node { int data; Node next;

Node(int new_data) {
    data = new_data;
    next = null;
}

}

class GfG {

static Node rotate(Node head, int k) {
    if (k == 0 || head == null)
        return head;

    // rotate the list by k nodes
    for (int i = 0; i < k; ++i) {
        Node curr = head;
        while (curr.next != null)
            curr = curr.next;

        // move the first node to the last
        curr.next = head;
        curr = curr.next;
        head = head.next;
        curr.next = null;
    }
    return head;
}

static void printList(Node node) {
    while (node != null) {
        System.out.print(node.data);
        if(node.next != null){
            System.out.print(" -> ");
        }
        node = node.next;
    }
    System.out.println();
}

public static void main(String[] args) {

    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);

    head = rotate(head, 6);
    printList(head);
}

}

Python

class Node: def init(self, newData): self.data = newData self.next = None

def rotate(head, k): if k == 0 or head is None: return head

# rotate the list by k nodes
for _ in range(k):
    curr = head
    while curr.next is not None:
        curr = curr.next
    
    # move the first node to the last
    curr.next = head
    curr = curr.next
    head = head.next
    curr.next = None

return head

def printList(node): while node is not None: print(node.data, end = "") if node.next != None: print(" -> ", end = "") node = node.next print()

if name == "main":

head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)

head = rotate(head, 6)
printList(head)

C#

using System;

class Node { public int data; public Node next;

public Node(int new_data) {
    data = new_data;
    next = null;
}

}

class GfG { static Node Rotate(Node head, int k) { if (k == 0 || head == null) return head;

    // rotate the list by k nodes
    for (int i = 0; i < k; ++i) {
        Node curr = head;
        while (curr.next != null)
            curr = curr.next;
        
        // move the first node to the last
        curr.next = head;
        curr = curr.next;
        head = head.next;
        curr.next = null;
    }
    return head;
}

static void printList(Node node) {
    while (node != null) {
        Console.Write(node.data);
        if(node.next != null){
            Console.Write(" -> ");
        }
        node = node.next;
    }
    Console.WriteLine();
}

static void Main() {
      
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);

    head = Rotate(head, 6);
    printList(head);
}

}

JavaScript

class Node { constructor(new_data) { this.data = new_data; this.next = null; } }

function rotate(head, k) { if (k === 0 || head === null) { return head; }

// rotate the list by k nodes
for (let i = 0; i < k; ++i) {
    let curr = head;
    while (curr.next !== null) {
        curr = curr.next;
    }

    // move the first node to the last
    curr.next = head;
    curr = curr.next;
    head = head.next;
    curr.next = null;
}
return head;

}

function printList(node) { let result = ''; while (node !== null) { result += node.data; if(node.next != null){ result += " -> "; } node = node.next; } console.log(result.trim()); }

// Driver Code let head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40);

head = rotate(head, 6); printList(head);

`

Output

30 -> 40 -> 10 -> 20

[Expected Approach] By changing pointer of kth node - O(n) Time and O(1) Space

The idea is to first convert the linked list to circular linked list by updating the next pointer of last node to the head of linked list. Then, traverse to the kth node and update the head of the linked list to the (k+1)th node. Finally, break the loop by updating the next pointer of kth node to NULL.

**How to handle large values of k?

For a linked list of size **n, if we rotate the linked list to the left by **n places, then the linked list will remain **unchanged and if we rotate the list to the left by ****(n + 1)** places, then it is same as rotating the linked list to the left by **1 place. Similarly, if we rotate the linked list k (k >= n) places to the left, then it is same as rotating the linked list by ****(k % n)** places. So, we can simply update k with k % n to handle large values of k.

Illustrations:

C++ `

#include using namespace std;

class Node { public: int data; Node *next;

Node(int new_data) {
    data = new_data;
    next = nullptr;
}

};

Node *rotate(Node *head, int k) {

if (k == 0 || head == nullptr)
    return head;

Node *curr = head;
int len = 1;

  // find the length of linked list
while (curr->next != nullptr) {
    curr = curr->next;
    len += 1;
}

  // modulo k with length of linked list to handle
  // large values of k
k %= len;

if (k == 0)
    return head;

  // make the linked list circular
curr->next = head;

  // traverse the linked list to find the kth node
curr = head;
for (int i = 1; i < k; i++)
    curr = curr->next;

  // update the (k + 1)th node as the new head
head = curr->next;

  // break the loop by updating next pointer of kth node
curr->next = nullptr;
return head;

}

void printList(Node *node) { while (node != nullptr) { cout << node->data << " "; if(node->next != NULL){ cout << "-> "; } node = node->next; } cout << endl; }

int main() {

Node *head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);

head = rotate(head, 6);
printList(head);
return 0;

}

C

#include <stdio.h>

struct Node { int data; struct Node *next; };

struct Node* createNode(int new_data) { struct Node new_node = (struct Node)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; return new_node; }

struct Node* rotate(struct Node *head, int k) {

if (k == 0 || head == NULL)
    return head;

struct Node *curr = head;
int len = 1;

// find the length of linked list
while (curr->next != NULL) {
    curr = curr->next;
    len += 1;
}

// modulo k with length of linked list to
// handle large values of k
k %= len;

if (k == 0)
    return head;

  // make the linked list circular
curr->next = head;

// traverse the linked list to find the kth node
curr = head;
for (int i = 1; i < k; i++)
    curr = curr->next;

// update the (k + 1)th node as the new head
head = curr->next;

// break the loop by updating next pointer
// of kth node
curr->next = NULL;
return head;

}

void printList(struct Node *node) { while (node != NULL) { printf("%d", node->data); if(node->next != NULL){ printf(" -> "); } node = node->next; } printf("\n"); }

int main() {

// create a hard-coded linked list:
// 10 -> 20 -> 30 -> 40
struct Node *head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
head->next->next->next = createNode(40);

head = rotate(head, 6);
printList(head);
return 0;

}

Java

class Node { int data; Node next;

Node(int new_data) {
    data = new_data;
    next = null;
}

}

class GfG { static Node rotate(Node head, int k) {

    if (k == 0 || head == null)
        return head;

    Node curr = head;
    int len = 1;
  
    // find the length of linked list
    while (curr.next != null) {
        curr = curr.next;
        len += 1;
    }

    // modulo k with length of linked list to handle
    // large values of k
    k %= len;
  
    if (k == 0)
        return head;
  
      // make the linked list circular
    curr.next = head;

    // traverse the linked list to find the kth node
    curr = head;
    for (int i = 1; i < k; i++)
        curr = curr.next;

    // update the (k + 1)th node as the new head
    head = curr.next;
  
    // break the loop by updating next pointer
    // of kth node
    curr.next = null;
    return head;
}

static void printList(Node node) {
    while (node != null) {
        System.out.print(node.data);
        if(node.next != null){
            System.out.print(" -> ");
        }
        node = node.next;
    }
    System.out.println();
}

public static void main(String[] args) {

    // create a hard-coded linked list:
    // 10 -> 20 -> 30 -> 40
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);
  
    head = rotate(head, 6);
    printList(head);
}

}

Python

class Node: def init(self, newData): self.data = newData self.next = None

def rotate(head, k):

if k == 0 or head is None:
    return head

curr = head
length = 1

# find the length of the linked list
while curr.next is not None:
    curr = curr.next
    length += 1

# modulo k with length of linked list to handle
# large values of k
k %= length

if k == 0:
    curr.next = None
    return head

# make the linked list circular
curr.next = head

# traverse the linked list to find the kth node
curr = head
for _ in range(1, k):
    curr = curr.next

# update the (k + 1)th node as the new head
newHead = curr.next

# break the loop by updating the next pointer
# of kth node
curr.next = None
return newHead

def printList(node): while node is not None: print(node.data, end="") if node.next != None: print(" -> ", end="") node = node.next

if name == "main":

# create a hard-coded linked list
# 10 -> 20 -> 30 -> 40
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)

head = rotate(head, 6)
printList(head)

C#

using System;

class Node { public int data; public Node next;

public Node(int new_data) {
    data = new_data;
    next = null;
}

}

class GfG {

static Node rotate(Node head, int k) {

    if (k == 0 || head == null)
        return head;

    Node curr = head;
    int len = 1;
  
    // find the length of linked list
    while (curr.next != null) {
        curr = curr.next;
        len += 1;
    }

    // modulo k with length of linked list to handle
    // large values of k
    k %= len;
  
    if (k == 0) 
        return head;
  
      // make the linked list circular
    curr.next = head;

    // traverse the linked list to find the kth node
    curr = head;
    for (int i = 1; i < k; i++)
        curr = curr.next;

    // update the (k + 1)th node as the new head
    Node newHead = curr.next;
  
    // break the loop by updating next pointer
    // of kth node
    curr.next = null;
    return newHead;
}

static void printList(Node node) {
    while (node != null) {
        Console.Write(node.data);
        if(node.next != null){
            Console.Write(" -> ");
        }
        node = node.next;
    }
    Console.WriteLine();
}

public static void Main() {

    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);
  
    head = rotate(head, 6);
    printList(head);
}

}

JavaScript

class Node { constructor(new_data) { this.data = new_data; this.next = null; } }

function rotate(head, k) {

if (k === 0 || head === null)
    return head;

let curr = head;
let len = 1;

// find the length of linked list
while (curr.next !== null) {
    curr = curr.next;
    len += 1;
}

// modulo k with length of linked list to handle
// large values of k
k %= len;

if (k === 0) {
    curr.next = null;
    return head;
}

// make the linked list circular
curr.next = head;

// traverse the linked list to find the kth node
curr = head;
for (let i = 1; i < k; i++)
    curr = curr.next;

// update the (k + 1)th node as the new head
let newHead = curr.next;

// break the loop by updating next pointer of kth node
curr.next = null;
return newHead;

}

function printList(node) { let output = ''; while (node !== null) { output += node.data; if(node.next !== null){ output += " -> "; } node = node.next; } console.log(output.trim()); }

// Driver Code

let head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40);

head = rotate(head, 6); printList(head);

`

Output

30 -> 40 -> 10 -> 20