Rotate a Linked List | Practice | GeeksforGeeks

Given a singly linked list of size N. The task is to left-shift the linked list by k nodes, where k is a given positive integer smaller than or equal to length of the linked list. 
Example 1:
Input:
N = 5
value[] = {2, 4, 7, 8, 9}
k = 3
Output: 8 9 2 4 7
Explanation:
Rotate 1: 4 -> 7 -> 8 -> 9 -> 2
Rotate 2: 7 -> 8 -> 9 -> 2 -> 4
Rotate 3: 8 -> 9 -> 2 -> 4 -> 7

Example 2:
Input:
N = 8
value[] = {1, 2, 3, 4, 5, 6, 7, 8}
k = 4
Output: 5 6 7 8 1 2 3 4


Your Task:
You don't need to read input or print anything. Your task is to complete the function rotate() which takes a head reference as the first argument and k as the second argument, and returns the head of the rotated linked list.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 103
1 <= k <= N
0
sarshdeep054in 12 hours
public Node rotate(Node head, int k) {       int n=0;       ArrayList<Node> al=new ArrayList<Node>();       Node ex=head;       while(head!=null){           al.add(head);           head=head.next;           n++;       }       if(n==k)       return ex;              int temp=k;       Node ret=al.get(k);       k=k%n;       Node[] arr=new Node[n];   for(int i=0;i<n;i++){       arr[(n-k+i)%n]=al.get(i);   }   for(int j=1;j<n;j++){       ret.next=arr[j];       ret=arr[j];   }   ret.next=null;   return al.get(temp);    }
0
taiphanvan2403in 4 hours
Node* rotate(Node* head, int k)
    {
        Node*tail=head;
        int cnt=0;
        Node* pivot=NULL;
        while(tail->next!=NULL){
            cnt++;
            if(cnt==k) pivot=tail;
            tail=tail->next;
        }
        if (pivot == NULL)
            return head;
        Node* tam=head;
        head=pivot->next;
        pivot->next=NULL;
        tail->next=tam;
        return head;
    }
0
vats7aryan012 days ago
Node* rotate(Node* head, int k)   {       Node* temp=head,*start=head,*end=head;       while(end->next!=NULL)           end=end->next;       while(k--)       {           end->next=start;           head=start->next;           start->next=NULL;           end=end->next;           start=head;       }       return head;   }
0
anushkakaushik2002192 days ago
*EASY CPP SOLUTION*
 
Node* rotate(Node* head, int k)   {       Node* temp=head;       Node* start=head;       Node* end=head;       while(end->next!=NULL)       {           end=end->next;       }       while(k>0)       {           end->next=start;           head=start->next;           start->next=NULL;           end=end->next;           start=head;           k--;       }       return head;   }
0
darkseid72 days ago
ONE PASS || C++ || O(N) || O(1)
Node* rotate(Node* head, int k){
        Node* i = head;
        Node* j = head;
        for(int y=1;y<k;y++)j=j->next;
        if(j->next==NULL){
            return head;
        }
        Node* l = j;
        while(l->next!=NULL)l=l->next;
        l->next=i;
        Node* Head = j->next;
        j->next=NULL;
        return Head;
    }
0
indusridharavath1232 days ago
 Node* rotate(Node* head, int k)   {       // Your code here       if(k==0)       {           return head;       }           Node* temp=head;       while(temp->next!=NULL)       {           temp=temp->next;                  }           while(k>0)       {           Node* temp2=new Node(head->data);           temp->next=temp2;           temp=temp2;           head=head->next;           k--;       }       return head;   }
+1
mdzama4 days ago
C++ || Simple & Clean || 1 Pass Solution
Node* rotate(Node* head, int k) {
        if (!k || !head || !head->next) return head;
        
        int position = 1;
        Node *save, *tail;
        save = tail = nullptr;
        
        Node* temp = head;
        while (temp) {  // In onepass solution.
            if (position == k) save = temp;
            if (!temp->next) tail = temp;
            temp = temp->next;
            position++;
        }
        if (save != tail) {
            tail->next = head;
            head = save->next;
            save->next = nullptr;
        }
        return head;
    }
0
pimplevaibhav016 days ago
// java Solutionclass Solution{   //Function to rotate a linked list.   public Node rotate(Node head, int k) {       // add code here       Node tail=head;       while(tail.next!=null){           tail=tail.next;       }       for(int i=0;i<k;i++){           tail.next =head;           Node fistNode=head.next;           head.next=null;           tail=head;           head=fistNode;                  }       //System.out.println(tail.data);              return head;   }}
0
shemanthkgp6 days ago
// C++ solution 
 
class Solution{   public:   //Function to rotate a linked list.   Node* rotate(Node* head, int k)   {       if(k==0){           exit(0);       }       Node* temp= head;       while(temp->next!=NULL){           temp = temp->next;       }       temp->next = head;       temp=head;       for(int i=0;i<k-1;i++){           temp=temp->next;       }       head = temp->next;       temp->next=NULL;       return head;          }   };
0
rajkumarraj1 week ago
class Solution
{
    public:
    //Function to rotate a linked list.
    Node* rotate(Node* head, int k)
    {
        // Your code here
        Node *temp;
        int c=1;
        temp=head;
        if(head==NULL||head->next==NULL)
        return head;
        else
        {
        while(temp->next!=NULL)
        {temp=temp->next;
        c++;
        }
        
        temp->next=head;
        temp=head;
        for(int i=1;i<c-k;i++)
        {
            temp=temp->next;
        }
        head=temp->next;
        temp->next=NULL;
        return head;
        }
        
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.