-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path25.cpp
More file actions
119 lines (101 loc) · 2.62 KB
/
25.cpp
File metadata and controls
119 lines (101 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Author : Accagain
// Date : 17/2/16
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
*
* k is a positive integer and is less than or equal to the length of the linked list.
*
* If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
*
* You may not alter the values in the nodes, only nodes itself may be changed.
*
* Only constant memory is allowed.
*
* For example,
*
* Given this linked list: 1->2->3->4->5
*
* For k = 2, you should return: 2->1->4->3->5
*
* For k = 3, you should return: 3->2->1->4->5
*
* 做法:
*
* 时间复杂度:
*
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define INF 0x3fffffff
using namespace std;
/**
* Definition for singly-linked list. */
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode * emptyNode = new ListNode(INF);
emptyNode->next = head;
ListNode * now = emptyNode;
while(true)
{
if(now == NULL)
break;
ListNode * tmp = now;
ListNode * ne;
for(int i=1; i<=k; i++)
{
ne = now->next;
if(ne == NULL)
break;
now = ne;
}
if(ne == NULL)
break;
now = tmp;
ListNode *t1 = now->next;
tmp = t1;
ListNode *t2 = t1->next;
for(int i=1; i<k; i++)
{
ListNode * next = t2->next;
t2->next = t1;
t1 = t2;
t2 = next;
}
now->next = ne;
tmp->next = t2;
now = tmp;
}
return emptyNode->next;
}
};
int main() {
Solution *test = new Solution();
int data[] = {1, 2, 3, 4, 5};
ListNode * emptyHead = new ListNode(INF);
ListNode * cur = emptyHead;
for(int i=0; i<5; i++)
{
ListNode * now = new ListNode(data[i]);
cur->next = now;
cur = cur->next;
}
ListNode * ans = test->reverseKGroup(emptyHead->next, 1);
while(ans)
{
printf("%d ", ans->val);
ans = ans->next;
}
return 0;
}