Advertisement
enigmjoe

Big O week 4 day 1 p1

Sep 4th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. Problem: https://www.codingninjas.com/studio/problems/920454?topList=striver-sde-sheet-problems&leftPanelTab=1
  2. Author: Sarthak Joleya
  3.  
  4. public class Solution {
  5. public static Node rotate(Node head, int k) {
  6. int n = 0;
  7. Node tmp = head;
  8. while(tmp != null){
  9. tmp = tmp.next;
  10. n++;
  11. }
  12. k %= n;
  13. if(k == 0) return head;
  14. tmp = head;
  15. Node ans=head;
  16. int x = n - k;
  17. while(x-- > 0){
  18. if(x == 0){
  19. ans = ans.next;
  20. tmp.next = null;
  21. break;
  22. }
  23. ans = ans.next;
  24. tmp = tmp.next;
  25. }
  26. tmp = ans;
  27. while(tmp.next != null){
  28. tmp = tmp.next;
  29. }
  30. tmp.next = head;
  31. return ans;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement