코테준비 - Reverse Nodes in k-Group

정상화·2023년 2월 26일

LeetCode

목록 보기
24/222

Reverse Nodes in k-Group


class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        vector<ListNode *> result;

        for (auto cursor = head; cursor != nullptr; cursor = cursor->next) {
            result.push_back(cursor);
        }
        result.push_back(nullptr);

        int sz = result.size();
        for (int i = 0; i < sz; i++) {
            if (i != 0 && i % k == 0) {
                reverse(result.begin() + i - k, result.begin() + i);
            }
        }
        for (auto it = result.begin(); it != result.end(); it++) {
            if (next(it) != result.end()) {
                (*it)->next = *next(it);
            }
        }

        return result.empty() ? NULL : result.front();
    }
};
profile
백엔드 희망

0개의 댓글