Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
reverseKNode
//Title of this code #include <iostream> using namespace std; struct Node { int data; Node* next; Node(int d, Node* n): data(d), next(n) {} }; void reverseAllKNode(Node*& n, int k) { Node *node = n; bool firstRun = true; Node* firstFirst; while (node) { Node *first = node; Node *prev = node; Node *cur = node->next; int length = k - 1; Node* tmp; while (cur && length > 0) { tmp = cur->next; cur->next = prev; prev = cur; cur = tmp; --length; } if (firstRun) { n = prev; firstRun = false; } if (firstFirst) firstFirst->next = prev; firstFirst = node; if (length > 0) { node->next = NULL; return; } first->next = tmp; node = tmp; } } void print(Node* n) { while (n) { cout << n->data << "->"; n = n->next; } cout << "NULL" << endl; } int main() { Node *start = new Node(1, new Node(2, new Node(3, new Node(4, new Node(5, new Node(6, new Node(7, new Node(8, NULL)))))))); //Node *start = new Node(1, new Node(2, new Node(3, new Node(4, NULL)))); print(start); reverseAllKNode(start, 3); print(start); }
run
|
edit
|
history
|
help
0
Dar
Lazy String Tokenizer Class
PriorQ
PLoshtina na krug
Mr
cs
initializer_list example
DFS
C++ Inheritance Example
MeanSDVar