Node*Reversek(Node*&head,int k){
//first we have to make three node pointers
Node *prevPtr = NULL;//the prev ptr points to Null
Node *currPtr = head;//the next ptr should points toward the prev ptr because we have to reverse
//the nodes this is possible if we point the next node towards the prev node
Node *nextptr=NULL;//we will assign the value to nextptr in the loop
int count = 0;
while(currPtr!=NULL && count<k)
{
nextptr = currPtr->next;
currPtr->next = prevPtr;
prevPtr = currPtr;
currPtr = nextptr;
count++;
}
if(nextptr!=NULL){
head->next = Reversek(nextptr, k);
}
return prevPtr;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter