Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
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 * n_adress ;


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

};

// creating a new class  , which is returning both head and tail....
class both{
public :
node * head ; 
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->n_adress = temp ;
            ft = temp ;
        }

        cin >> data ;
    }

    return fh ;
}
// reversing the LL....
both reversing (node * head ){
    if(head == NULL ||head->n_adress == NULL){
        both ans ;
        ans.head = head ;
        ans.tail = head ;
        return ans ; 
}
        both smallans = reversing(head->n_adress) ; 
        smallans.tail->n_adress = head ;
        head->n_adress = NULL ;
        smallans.tail = head ;
        return smallans ; 
    
}


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

}

void print(node * head){
    node * temp = head ;
    while(temp != NULL){
        cout << temp->data << " " ;
        temp = temp->n_adress ; 
    
    }
    cout << endl; 
    return ;

}

int main(){

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


}
Posted
Updated 22-Nov-21 7:55am
v4
Comments
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.

1 solution

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 ){
    if(head == NULL){
        return NULL ; 
    }
    int j = 1 ;
    node * h1 = head ;
    node * t1 = head ;
    while(j<k){
        if(t1->n_adress == NULL){
            break ; 
        }
        t1 = t1->n_adress ;
        j++ ; 
    }
    node * h2 = t1->n_adress ;
    t1->n_adress = NULL ;
    both smallans = reversing(h1) ;

    cout << "after reversing" << endl;   // added code
	print( smallans.head );              // added code

    node * so = k_reverse(h2 , k) ;
    smallans.tail->n_adress = h2 ;
    return smallans.head ;
}
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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900