题解 | #旋转链表#
旋转链表
https://www.nowcoder.com/practice/1ad00d19a5fa4b8dae65610af8bdb0ed
class Solution { public: ListNode* rotateLinkedList(ListNode* head, int k) { if(!head) return head; ListNode *p = head; int len = 1; while(p->next) { p = p->next; len++; } if(k==len) return head; k = k%len; p = head; for(int i = 0;i<len-k-1;i++) { p = p->next; } ListNode*t = p->next; while(t->next) t= t->next; t->next = head; head = p->next; p->next = nullptr; return head; } };