Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
reverseKNode
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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, new Node(5, new Node(6, NULL)))))); print(start); reverseAllKNode(start, 3); print(start); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.34 sec, absolute running time: 0.15 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.62 sec
edit mode
|
history
|
discussion
1->2->3->4->5->6->7->8->NULL 3->2->1->6->5->4->8->7->NULL