Microsoft Interview Question
Software Engineer / DevelopersHow about this?
struct node
{
void * data;
struct Node * Prev;
struct Node * Next;
};
struct node * Prev = NULL;
struct node * Next = NULL;
struct node * Cur = Head;
while(Cur != NULL)
{
Next = Cur -> Next;
Cur -> Next = Cur -> Prev;
Cur -> Prev = Next;
if(Next != NULL)
Cur = Next;
}
Head = Cur;
struct node
{
void * data;
struct Node * Prev;
struct Node * Next;
};
struct node * Cur = Head;
struct node * Temp = NULL;
while(Cur!=NULL){
if(Cur->Prev==NULL){//First Node
Cur->Prev=Cur->Next;
Cur->Next=NULL;
}
Temp=Cur->Next;
Cur->Next=Cur->Prev;
Cur->Prev=Temp;
Cur=Cur->Prev;
}
return Cur;
I thought about the code I wrote and its a complete blunder.
So here comes the right code.
struct node
{
void * data;
struct Node * Prev;
struct Node * Next;
};
struct node * Cur = Head;
struct node * Temp = NULL;
while(Cur!=NULL){
Temp=Cur->Next;
Cur->Next=Cur->Prev;
Cur->Prev=Cur->Temp;
Cur=Cur->Prev;//Important
}
return Cur; //Head
//@CyberPhoenix
//my dear i think when u had returened Cur its NULL
//is that of any significance
//well have a look on this!!
struct node
{
void * data;
struct Node * prev;
struct Node * next;
};
struct node * tra = Head;
while(tra){
head= tra->prev;
tra->prev = tra->next;
tra->next = head;
tra = tra->prev;
}
head = head->prev;
//Iterative
void reverdll(node *root){
node *cur=root;
node *res=NULL;
while(cur){
temp=cur->next;
cur->next=cur->prev;
cur->prev=temp;
cur=temp;
}
*root=cur;
}
//Recursion
node* reversedll(node *root){
if(root==NULL) return NULL;
else{
root->prev=reversedll(root->next);
root->next=root->prev;
return root;
}
}
struct node
- Rohit Ghatol April 15, 2006{
void * data;
struct Node * Prev;
struct Node * Next;
};
struct node * Prev = NULL;
struct node * Next = NULL;
struct node * Cur = Head;
while(Cur != NULL)
{
Next = Cur -> Next;
Cur -> Next = Prev;
Cur -> Prev = Next;
Prev = Cur;
Cur = Next;
}
Head = Prev;