赞
踩
题目:
题解:
- class Solution {
- public:
- ListNode* rotateRight(ListNode* head, int k) {
- if (k == 0 || head == nullptr || head->next == nullptr) {
- return head;
- }
- int n = 1;
- ListNode* iter = head;
- while (iter->next != nullptr) {
- iter = iter->next;
- n++;
- }
- int add = n - k % n;
- if (add == n) {
- return head;
- }
- iter->next = head;
- while (add--) {
- iter = iter->next;
- }
- ListNode* ret = iter->next;
- iter->next = nullptr;
- return ret;
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。