Interview Question
- -1of 1 vote
Answerswhy its not working?
i am reversing nodes of link list in blocks of k.
here is my codeint hasnode(struct node *s,int k) //checks if k node exist { int i; for(i=1;s&&i<k;i++) s=s->next; if(i==k) return 1; else return 0; } struct node* getkplusoneth_node(struct node **nextnode,int k)//returns k+1th node { int i; for(i=0;*nextnode&&i<k;i++) *nextnode=*nextnode->next; if(i==k) return *nextnode; } void reverseinkblocks(int k) //reverse function { int i; struct node* prev=NULL,*current=head,*nextnode=current,*temp; while(current&&hasnode(current,k)) { *nextnode=getkplusoneth_node(&nextnode,k); while(current!=nextnode)//reverse upto one node before nextnode { temp=current->next; current->next=prev; prev=current; current=temp; } }
}
- pddevesh March 01, 2015 in India| Report Duplicate | Flag | PURGE
C
while(current != nextnode)
- ABCD March 02, 2015{
temp = current->next
prev = current
temp->next = prev
//prev = current
current = current->next
}