Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem: https://www.codingninjas.com/studio/problems/920454?topList=striver-sde-sheet-problems&leftPanelTab=1
- Author: Sarthak Joleya
- public class Solution {
- public static Node rotate(Node head, int k) {
- int n = 0;
- Node tmp = head;
- while(tmp != null){
- tmp = tmp.next;
- n++;
- }
- k %= n;
- if(k == 0) return head;
- tmp = head;
- Node ans=head;
- int x = n - k;
- while(x-- > 0){
- if(x == 0){
- ans = ans.next;
- tmp.next = null;
- break;
- }
- ans = ans.next;
- tmp = tmp.next;
- }
- tmp = ans;
- while(tmp.next != null){
- tmp = tmp.next;
- }
- tmp.next = head;
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement