Leetcode 61. Rotate List | | Leetcode 61. Rotate List 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution12345678910111213141516171819202122232425262728293031323334/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* rotateRight(ListNode* head, int k) { if(!head) { return head; } int n = 0; ListNode* pre = nullptr; ListNode* current = head; while(current) { n++; pre = current; current = current->next; } pre->next = head; int target = n - k % n; current = head; while(target) { target--; pre = current; current = current->next; } pre->next = nullptr; return current; }}; Reference https://leetcode.com/problems/rotate-list/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏