Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- ListNode* rotateRight(ListNode* head, int k) {
- if(!head) return head;
- int len = 1;
- ListNode *newHead, *curr;
- curr = newHead = head;
- while(curr->next){
- len++;
- curr = curr->next;
- }
- curr->next = head;
- if(k%=len){
- for(int i=0; i<(len-k); i++)
- curr = curr->next;
- }
- newHead = curr->next;
- curr->next = NULL;
- return newHead;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement