61. Rotate List

    xiaoxiao2021-03-26  22

    题意简单,但是但是!!!! 超级多小细节,一定要二刷!!!! 练手好体!!!

    /** * 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) { int n = 1; if(head == NULL || head -> next == NULL) return head; ListNode* t; ListNode* u; ListNode* newhead; for(t = head; t -> next != NULL; t = t -> next) n++; if(n == k) return head; k = k % n; if(k == 0) return head; t = head; for(int i = 1; i < n - k; ++ i) t = t -> next; u = t -> next; newhead = u; t -> next = NULL; while(u -> next != NULL) u = u -> next; u -> next = head; return newhead; } };
    转载请注明原文地址: https://ju.6miu.com/read-650262.html

    最新回复(0)