Pranchal Agrahari
Last Updated: May 13, 2022

## Introduction

A linked list is a linear data structure that consists of nodes. Each Node contains a data field and a pointer to the next Node. In Linked List, unlike arrays, elements are not stored at contiguous memory locations but rather at different memory locations. The various elements in a linked list are linked together using pointers.

Linked List is one of the important topics from an interview perspective. Almost all the major companies ask questions related to Linked List in the initial stages. One of the most frequently asked questions by Top Product based companies, including Amazon, Flipkart, Adobe, Goldman Sachs, is “Rotate a Linked List.”

The problem statement is: Given a Linked List and an integer ‘K.’ Rotate the Linked List by ‘K’ positions in a clockwise direction from the last node.”

For example, consider the given linked list is 1 -> 2 -> 3 -> 4 and K is 2 then the modified linked list after K rotation is 3 -> 4 -> 1-> 2. Notice that we have moved two-node from last to the front of the linked list

## Approach 1

To rotate a linked list by K position first thing that comes to mind is to move the K node from the back of the linked list to the front of the linked list, but there’s a problem in this approach if K is greater than the length of linked list then we will be moving same nodes multiple times so firstly we will check that whether K is smaller than the length of the linked list or not. If K is greater than the length of the linked list, then take the modulo of K with the length of the linked list(K%length). Now we can move K nodes from the back of the linked list to the front. But it is more complicated to find K nodes from the end than finding nodes from the front, so we will find the subtract K from the length, and now the problem changes to moving nodes from the front of the linked list to the back, i.e., left rotation of linked list.

## Algorithm

1. If the head is equal to null or ‘K’ is equal to 0, return the Linked List head.
2. Create a variable that will store the length of the linked list and initialize it to 1.
3. Create a temporary node that will be used to find the length of the Linked List and initialize it to head.
4. Run a loop while the temporary node next is not equal to null and increase the length by 1 on each iteration
5. If length is less than ‘K,’ then update ‘K’ as ‘K’ % length ( To make ‘K’ come in a range of 0 to length).
6. Update ‘K’ as length - ‘K’ (To reach (‘K’+1)th node from the end).
7. If ‘K’ is equal to the length, return head.
8. Create a variable count, which will be used to reach (‘K’ + 1)th node from the end and initialize it to 1.
9. Create a node current that will store the current node of Linked List and initialize it to head.
10. Run a loop while the count is less than ‘K’ and the current node is not equal to ‘NULL.’
11. If the node is equal to ‘NULL.,’, then return the head of the Linked List.
12. Update temp->next as head and update head as current->next and current->next as ‘NULL.’

## Code:

Output:

Time Complexity: O(n) where n is the number of nodes in the linked list

Space complexity: O(1)

## Approach 2

This approach to rotate a linked list is similar to the previous one the only difference is that we will convert the singly linked list to a circular linked list and then move (length-K) nodes forward from the head node, making (length-K)th node’s next to null and next node as head.

Code:

Output:

Time Complexity: O(n) where n is number of nodes in linked list

Space complexity:O(1)

1. Can a linked list be circular?

Yes, a linked list can be circular in a circular linked list the last node points to the first node instead of NULL.

2.  How do I rotate a list?

To rotate a linked list, you need to move the nodes of the linked list in the clock or counter-clockwise; you can do so by moving nodes from front to back or moving nodes from back to the front of the linked list.

3.  What does it mean to rotate a list?

Rotating a linked list  means considering a linked list as a circular structure and moving the node clock or anti-clockwise according to the requirement.

## Key Takeaways

This blog discussed a very frequently asked interview problem, “Rotate a linked list.” The blog discussed two methods to solve this problem with algorithms and code in c++ if you want to master the linked list then check out this article.

If you are a beginner in coding and want to learn DSA, you can look out for our guided path for DSA, which is absolutely free!

If you want to solve more problems like this which have been asked in the interviews, big tech giants like Amazon, Flipkart, Google, and Facebook, you can look out for interview problems at Code Studio.

Happy Learning!
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}