-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathReverse Nodes in k-Group.cpp
85 lines (67 loc) · 2.14 KB
/
Reverse Nodes in k-Group.cpp
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
/*
Solution by Rahul Surana
***********************************************************
Given the head of a linked list, reverse the nodes of the list k at a time, and return the 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 list's nodes, only nodes themselves may be changed.
***********************************************************
*/
#include <bits/stdc++.h>
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void print(ListNode* &r){
ListNode* t= r;
while(t) {cout << t->val <<" -> "; t = t->next;}
cout <<"\n";
}
ListNode* reverse(ListNode* &head, ListNode* &end){
if(head == NULL) return NULL;
ListNode* prev = NULL;
ListNode* tail = head;
ListNode* nexter = NULL;
while(tail != end){
nexter = tail->next;
tail->next = prev;
prev = tail;
tail = nexter;
}
head->next = end;
return prev;
}
ListNode* reverseKGroup(ListNode* &head, int k) {
if(!head || !head->next) return head;
ListNode* t1 = head;
ListNode* t2 = head;
ListNode* r = NULL;
ListNode* prev = head, *np = NULL;
int c = 1;
while(t2){
while(c < k){
t2 = t2->next;
if(!t2) break;
c++;
}
if(!t2) break;
if(np) np->next = t2;
t2 = t2->next;
np = prev;
t1 = reverse(t1,t2);
if(!r) r = t1;
prev = t2;
t1 = t2;
c = 1;
}
return r;
}
};