61. Rotate List

    xiaoxiao2026-05-16  7

    方法1:

    链表首尾相连,将第len个节点的下一个节点设为NULL即可,返回第len+1个节点

    class Solution { public: ListNode* rotateRight(ListNode* head, int k) { if(head==NULL||head->next==NULL) return head; int len=1; ListNode* tail=head,*pre=head; while(tail->next) { tail=tail->next; len++; } k=k%len; k=len-k; while(k>1) { pre=pre->next; k--; } tail->next=head; head=pre->next; pre->next=NULL; return head; } };

    转载请注明原文地址: https://ju.6miu.com/read-1309752.html
    最新回复(0)