From c8332b97037ea9baca99091a0412c3854d6bfe0f Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Thu, 21 Jun 2018 22:32:26 +0800 Subject: [PATCH] Create 25.cpp --- LeetCode-CN/25.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 LeetCode-CN/25.cpp diff --git a/LeetCode-CN/25.cpp b/LeetCode-CN/25.cpp new file mode 100644 index 0000000..d19dfb0 --- /dev/null +++ b/LeetCode-CN/25.cpp @@ -0,0 +1,70 @@ +#include +class Solution { +public: + ListNode * reverseKGroup(ListNode* head, int k) { + ListNode* seg_start = head; + ListNode* now = head; + ListNode* return_head = head; + ListNode* last_seg = NULL; + int passed = 0; + + while (now != NULL) + { + passed++; + if (passed == k) + { + // Meet a segment + ListNode* seg_end = now; + // Change head + if (seg_start == head) + { + return_head = seg_end; + } + + ListNode* p = seg_start; + std::stack stk; + while (p != seg_end) + { + stk.push(p); + p = p->next; + } + // p is now seg_end + + // checkpoint + ListNode* next_seg_start = seg_end->next; + + while (!stk.empty()) + { + p->next = stk.top(); + stk.pop(); + p = p->next; + } + + // stack is empty. p is now seg_start. + // p->next now pointes to the orignal "next" + + // set last segment + if (last_seg) + { + last_seg->next = seg_end; + } + last_seg = seg_start; + // set it to next_seg_start + p->next = next_seg_start; + + seg_start = next_seg_start; + now = next_seg_start; + passed = 0; + + // debug + //printList(return_head); + } + else + { + now = now->next; + } + } + + return return_head; + } +};