/**
* 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) {
int all=1;
if(head == nullptr) return head;
ListNode* n_head=head;
while(n_head->next!=nullptr){
all++;
n_head=n_head->next;
}
n_head->next = head;
all = all - k%all;
while(all!=1){
all--;
head=head->next;
}
ListNode* new_head=head;
head = head->next;
new_head->next = nullptr;
return head;
}
};
leetcode61:旋转链表
最新推荐文章于 2022-07-02 11:38:17 发布