15,615,016 members
1.00/5 (1 vote)
See more:
K reverse , we have to reverse elements in group of k , the return the final linked list
example :
LL : 1234567 & K = 3
OUTPUT : 3216547
Problem in my method :
It isn't showing any error just the output is not matched and I can't find out the problem .
My input : 1 2 3 4 5 6 7 8
My output : 3 2 1 4 7
May I know what is wrong in my code ?

What I have tried:

```#include<iostream>
using namespace std;

class node{
public:
int data ;

node (int data ){
this->data = data ;
}

};

// creating a new class  , which is returning both head and tail....
class both{
public :
node * tail ;

};

node * take_input(){
int data ;
cin >> data ;
node * fh = NULL ;
node * ft = NULL ;
while(data != -1){
node * temp = new node(data) ;
if(fh == NULL){
fh = temp ;
ft = temp ;
}else{
ft = temp ;
}

cin >> data ;
}

return fh ;
}
// reversing the LL....
both reversing (node * head ){
both ans ;
return ans ;
}
return smallans ;

}

node * k_reverse (node * head , int k ){
return NULL ;
}
int j = 1 ;
node * h1 = head ;
node * t1 = head ;
while(j<k){
break ;
}
j++ ;
}
node * h2 = t1->n_adress ;
both smallans = reversing(h1) ;
node * so = k_reverse(h2 , k) ;

}

node * temp = head ;
while(temp != NULL){
cout << temp->data << " " ;

}
cout << endl;
return ;

}

int main(){

node * head = take_input() ;
int k ;
cin >> k  ;
node * f_ans = k_reverse(head , k ) ;
print(f_ans) ;

}```
Posted
Updated 22-Nov-21 7:55am
v4
OriginalGriff 22-Nov-21 2:36am
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 22-Nov-21 3:53am
You just need a couple of loops. Start by extracting the next K elements into a new list. Once that is complete you can print them in reverse order. Continue with the next list (or the remaining items if fewer) until all items have been processed.

## Solution 1

You almost have it. I added a couple lines of code for diagnostic purposes and they showed clearly what was going on. Here's the code I added :
C++
```node * k_reverse (node * head , int k ){
return NULL ;
}
int j = 1 ;
node * h1 = head ;
node * t1 = head ;
while(j<k){
break ;
}
j++ ;
}
node * h2 = t1->n_adress ;
both smallans = reversing(h1) ;

cout << "after reversing" << endl;   // added code

node * so = k_reverse(h2 , k) ;
}```
I also moved the print function to be above this one and it gave this output :
```1 2 3 4 5 6 7
after reversing
3 2 1
after reversing
6 5 4
after reversing
7```
It became clear that the lists were not spliced together correctly. I changed exactly one line and everything worked. Here's that line :
C++
```smallans.tail->n_adress = so;   // was h2;
```