LCR 141. 训练计划 III - 力扣(LeetCode)
非递归
class Solution { public: ListNode* trainningPlan(ListNode* head) { if(head != nullptr && head->next != nullptr) { ListNode* former = nullptr; ListNode* mid = head; ListNode* laster = nullptr; while(mid != nullptr) { laster = mid->next; mid->next = former; former = mid; mid = laster; } return former; } else { return head; } } };
还没有评论,来说两句吧...