What if K is grater than N in this question?

Hello, I have simply returned the original list without changing if k > N but I an getting only half of the cases passed. Please help me where I am wrong.

Here is my code:


#include
using namespace std ;

class node{
public:
int data ;
node * next ;

node(int d){
	data = d ;
	next = NULL ;
}

} ;

void insert_at_Tail(node* &head, int d){
if(head == NULL){
head = new node(d) ;
return ;
}

node* temp = head ;
while(temp -> next != NULL){
	temp = temp-> next;
}

node * n = new node(d) ;
temp-> next = n ;

}

void append(node* &head,int n, int k){

if(k >= n ) return  ;

node* slow = head ;
node* fast = head ;
for(int i = 0; i < k; i++){
	fast = fast -> next ;
}

while(fast -> next != NULL){
	slow = slow -> next ;
	fast = fast -> next ;
}
fast -> next = head ;
head = slow -> next ;
slow -> next = NULL ;

}

int main() {

int n ; 
cin >> n ;

node * head = NULL ;
int l = n ;

while(l--){
	int t;
	cin >> t ;
	insert_at_Tail(head,t)  ;
}

int k ;
cin >> k ;

append(head,n,k) ;
node* temp = head ;
while(temp != NULL){
	cout << temp -> data <<" " ;
	temp = temp -> next ;
}

return 0;

}

Hey @akabhi83229 i would request you to save your code at ide.codingblocks.com and share you link here :smile:

Hey @akabhi83229 :smile:,
the program you made is not appropriate for the problem statement.
with an input of :
7
9 4 1 7 8 3 2 6 5
3
it’s giving output of :
4 1 7 8 3 2 9
but it should give :
1 4 9 3 8 7 5 6 2
i’ll explain you the problem statement which is to reverse every k nodes (where k is an input to the function) and k is a factor of size of List.

Reverse the first sub-list of size k.
1.1 While reversing keep track of the next node and previous node.
1.2 Let the pointer to the next node be next and pointer to the previous node be prev.
head->next = reverse(next, k) /* Recursively call for rest of the list and link the two sub-lists */
return prev
Now prev becomes the new head of the list (see the iterative method)
N = 9 & K = 3

Original LL is : [9 -> 4 -> 1] -> [7 -> 8 -> 3] -> [2 -> 6 -> 5]

After k Reverse : [1 -> 4 -> 9] ->[ 3 -> 8 -> 7] ->[ 5 -> 6 -> 2]

Whereas for K greater then N you will have 2 cases and they are that k==N in that case , reverse the entire list, whereas when k>N then do k%N, and then you just have to reverse them.
Hope it will help you :smile:

Hello , I guess you got wrong question, this code is for appending last k elements in the list to the front but you though it as K-reverse. One more thing for the test case you mentioned you initialised list with 7 elements but gave 9 inputs that’s why it was append last 3 out of 7 to the front.

hey @akabhi83229 sorry for inconvenience,
i am having some technical glitch on my end , moreover i have edited one line in your code where you returned when k>n i have called the same function while using append(head,n,k%n). Now it will pass all test cases.
Thank You :smile:

Thanks the issue got resolved by the modulo, i missed that.

If this was helpful don’t forget to hit like button :grinning: