Create 25.cpp

This commit is contained in:
Kirigaya Kazuto 2018-06-21 22:32:26 +08:00 committed by GitHub
parent 20e17bc39e
commit c8332b9703
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

70
LeetCode-CN/25.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <stack>
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<ListNode*> 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;
}
};