Goldman Sachs Interview Question
Software Engineer / Developersnode * prevoius = NULL;
node * current = NULL;
node * following = NULL;
prevoius = head;
current = prevoius->next;
following = current->next;
prevoius->next = NULL;
while(following != NULL)
{
current->next = prevoius;
prevoius = current;
current = following;
following = following->next;
if(following == NULL)
{
current->next = prevoius;
}
}
head = current;
printList(head);
void reversePrint(node *n)
- JZ January 10, 2007{
if(n)
{ reversePrint(n->next);
print(n->content);
}
}
does the job, but function calls incur a lot of stack space
Other way would be to reverse the linked list,print the list and reverse the list again to restore it
/*reversal*/
void reverseList(node ** head)
{
node* curr = *head, *prev = null;
while(curr)
{
nxt_fwd = curr->next;
curr->next = prev;
prev =curr;
curr=nxt_fwd;
}
*head = prev; /*save the new head */
}