Linked list K append

Code - https://ide.codingblocks.com/s/38032
Ques - https://hack.codingblocks.com/contests/c/511/316
Can Someone Tell me why My logic is going wrong ?

Your logic is wrong as firstly you have to
traverse (n-k-1) nodes
then put the (n-k-1)th node’s -> next = null
reinitialise (n-k)th node as head
then traverse the list till the earlier tail and put tail -> next = older head.
and return the new head at last.
also handle the cases when k > n.

@sanjeetboora Thanks, I figured out that i was returning -1 as value of n, while it was being decremented in loop and i was returning that n for next use thinking that its value is as previous … . so it was disturbing the whole function else my logic part was correct and now output is also correct.
You Can check - https://ide.codingblocks.com/s/38144

Hey! i have checked your code… only one thing which you needs to handle in your code is when K > N.

Actually I tried hard to handle that case but it didn’t work. This was my code for that https://ide.codingblocks.com/s/38271
If you can suggest me some another way to achieve the K > N then it would be helpfull :slightly_smiling_face:

Try appending k%N elements instead of k elements when k>N.

1 Like

Thanks @Ashwin , that worked :smile:
This is my final code - https://ide.codingblocks.com/s/38289

Hey! this link is blank. It does not contain code.

https://ide.codingblocks.com/s/38289
???

@Unique-Jain-90707534 Its Working here . . . Try to open it in mozilla firefox 2 or 3 times .

Hey, this link is actually blank… it’s having the default ide code.

Yeah but it is working in mine, it may be a bug in CB ide
I am unable to save my code to make a link . . . so that’s why pasting whole code here :
#include
using namespace std;
class node{
public:
int data;
node* next;

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

};
void insertAtTail(node*&head,int data){
if(head == NULL){
head = new node(data);
return;
}
node tail = head;
while(tail->next != NULL){
tail = tail->next;
}
tail->next = new node(data);
}
int buildList(node
&head){
int n;
cin>>n;
int a=n;
while(n–){
int data;
cin>>data;
insertAtTail(head,data);
}
return a;
}
void printList(nodehead){
if(head == NULL){
return;
}
cout<data<<" ";
printList(head->next);
}
node
checkList(nodehead,int k,int n){
node
last;
last = head;
int i=0;
k = k%n;
int ok = n-k-1;
while(last != NULL && i<ok){
last = last -> next;
i++;
}
nodesecond = last->next;
last->next = NULL;
return second;
}
int main() {
int k;
node
head = NULL;
int n = buildList(head);
cin>>k;
node* temp = checkList(head,k,n);
printList(temp);
printList(head);
}

1 Like

My code is not evaluating on your platform but it is working fine on others.

kindly see to it

code link- https://ide.codingblocks.com/s/48170

Hi Tanay, since the question says that k can be greater than n, after you take k as input, you should do something like k=k%n. This would reduce the complexity considerably.

Also, your code was giving wrong answer when k=0, so I’ve added an if condition to simply print the linked list as it is when k=0. I’ve made all these changes and now the code works fine. You can check them here: https://ide.codingblocks.com/s/67032.

when k>n we did k=k%n
But Why?

I am getting sengmentation fault dont know why

I can’t see your code. Please re-post.

Coding Blocks IDE

Only two test case have passed and i am getting a run error for all others
can you please check the code and give me feedback where i can improve