Rotate List

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

Example:

Input:  1->2->3->4->5->NULL ,k=2
Output: 4->5->1->2->3->NULL

Approach

Java

public class RotateList {
    public static void main(String[] args) {
        ListNode l = new ListNode(1);
        l.next = new ListNode(2);
        l.next.next = new ListNode(3);
        l.next.next.next = new ListNode(4);
        l.next.next.next.next = new ListNode(5);
        int k = 2;
        ListNode rotate = rotateRight(l, k);
        linkedListTraversal(rotate);
    }

    static ListNode rotateRight(ListNode headint k) {
        int n = 0;
        ListNode temp = head;
        ListNode temp1 = head;
        if (temp == null || k == 0)
            return temp;
        while (temp != null) {
            temp = temp.next;
            n++;
        }
        k = k % n;
        if (k == 0)
            return head;
        int x = n - k;
        ListNode prev = head;
        while (temp1 != null && x > 0) {
            prev = temp1;
            temp1 = temp1.next;
            x--;
        }
        prev.next = null;
        ListNode p = temp1;
        while (temp1.next != null)
            temp1 = temp1.next;
        temp1.next = head;
        head = p;
        return head;
    }

    private static void linkedListTraversal(ListNode node) {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
    }
}


class ListNode{
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int valListNode next) {
        this.val = val;
        this.next = next;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
//Struture of  node
struct Node
   int data;
   Node *next;
   Node(int data)
    {
        this->data=data;
        this->next=NULL;
    }
};

Node* rotateRight(Node* headint k
{
        int n=0;
        Node *temp=head,*temp1=head;
        if(temp==NULL||k==0)
              return temp;
        while(temp!=NULL)
          { 
             temp=temp->next;
             n++;
          }
        k=k%n;
        if(k==0)
             return head;
        int x=n-k;
        Node *prev=head;
        while(temp1&&x)
        {
            prev=temp1;
            temp1=temp1->next;
            x--;
        }
         prev->next=NULL;
        Node *p=temp1;
        while(temp1->next!=NULL)
            temp1=temp1->next;
        temp1->next=head;
        head=p;
        return head;
}
//function to traverse(print) a linked 
//list
void linkedListTraversal(Node *head)
{
    Node *temp=head;
    //iterate till we reach end of the linked 
    //list
    while(temp!=NULL)
     {
         //print the current
         //node data
         cout<<temp->data<<" ";
         //move to next node
         temp=temp->next;
     }
}
int main()
{
    Node *head=new Node(1);
    head->next=new Node(2);
    head->next->next=new Node(3);
    head->next->next->next=new Node(4);
    head->next->next->next->next=new Node(5);
    int k=2;
    head=rotateRight(head,k);
    linkedListTraversal(head);
    return 0;

}


No comments:

Post a Comment