Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 16f4dd8

Browse files
solved leetcode daily challenge, reverse nodes in k-groups
1 parent 4f2bb30 commit 16f4dd8

File tree

1 file changed

+85
-0
lines changed
  • LeetCode/Reverse_Nodes_in_k-Group

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <bits/stdc++.h>
2+
#include <gtest/gtest.h>
3+
using namespace std;
4+
5+
6+
//// START
7+
/*
8+
## Reverse Nodes in k-Group
9+
10+
*/
11+
12+
13+
14+
/**
15+
* Definition for singly-linked list.
16+
*/
17+
18+
struct ListNode {
19+
int val;
20+
ListNode *next;
21+
ListNode() : val(0), next(nullptr) {}
22+
ListNode(int x) : val(x), next(nullptr) {}
23+
ListNode(int x, ListNode *next) : val(x), next(next) {}
24+
};
25+
26+
class Solution {
27+
public:
28+
ListNode *reverseKGroup(ListNode *head, int k) {
29+
if (!head) return nullptr;
30+
if (k == 1) return head;
31+
32+
int i = 0;
33+
ListNode *tail = head;
34+
while (tail) {
35+
i++;
36+
if (i == k) break;
37+
tail = tail->next;
38+
}
39+
if (i < k) return head;
40+
41+
auto *nn = tail->next;
42+
ListNode *last = nullptr;
43+
ListNode *cur = head;
44+
int ki = 0;
45+
while (ki < k) {
46+
ListNode *tmp = cur->next;
47+
cur->next = last;
48+
last = cur;
49+
cur = tmp;
50+
ki++;
51+
}
52+
head->next = reverseKGroup(nn, k);
53+
return tail;
54+
}
55+
};
56+
57+
//// END
58+
struct T {
59+
60+
};
61+
62+
TEST(Solution, test) {
63+
T ts[] = {
64+
{
65+
66+
},
67+
{
68+
69+
},
70+
71+
};
72+
73+
for (T t : ts) {
74+
Solution solution;
75+
76+
}
77+
}
78+
79+
int main() {
80+
testing::InitGoogleTest();
81+
82+
return RUN_ALL_TESTS();
83+
}
84+
85+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /