Microsoft Interview Question
Software Engineer in Testsnode* func(node* head)
{
if(!head||!head->next)
return head;
node* curr=head;
node* temp;
head=curr->next;
while(curr->next)
{
temp=curr->next;
curr->next=curr->next->next;
temp->next=curr;
curr=curr->next;
}
return head;
}
this is correct one.
node* func(node* head)
{
if(!head||!head->next)
return head;
node* curr=head;
node* temp;
head=curr->next;
while(curr&&curr->next)
{
temp=curr->next;
curr->next=curr->next->next;
temp->next=curr;
curr=curr->next;
}
return head;
}
Not correct ,
e.g, list a->b->c->d->e->f->g
//curr = a, curr->next = b
while(curr&&curr->next)
{
temp=curr->next; // temp = b
curr->next=curr->next->next; // a -> c
temp->next=curr; // b -> a
curr=curr->next; // cur = c
}
so after the first loop
b-> a -> c -> d -> e -> f
after the second loop
b-> a -> c
d-> c -> e -> f not correct!!!
//memcpy considering overlap
void* memcpy(void* dst,void* src,int count)
{
//no overlappoing or overlapping happens at the lower address of the source
if(((char*)dst>(char*)src+count)&&(dst<src))
{
while(count)
{
*(char*)dst=*(char*)src;
dst=(char*)dst+1;
src=(char*)src+1;
count--;
}
}
else
{
//there is overlap at the higher address of the source, then start copying from the higher address
dst=(char*)dst+count-1;
src=(char*)src+count-1;
while(count)
{
*(char*)dst=*(char*)src;
dst=(char*)dst+1;
src=(char*)src+1;
count--;
}
}
}
sorry a typo:
//memcpy considering overlap
void* memcpy(void* dst,void* src,int count)
{
//no overlappoing or overlapping happens at the lower address of the source
if(((char*)dst>(char*)src+count)&&(dst<src))
{
while(count)
{
*(char*)dst=*(char*)src;
dst=(char*)dst+1;
src=(char*)src+1;
count--;
}
}
else
{
//there is overlap at the higher address of the source, then start copying from the higher address
dst=(char*)dst+count-1;
src=(char*)src+count-1;
while(count)
{
*(char*)dst=*(char*)src;
dst=(char*)dst-1;
src=(char*)src-1;
count--;
}
}
}
Works perfectly - check it
Node* swapNodes(){
Node* cnode = head;
Node* tempnode1;
Node* tempnode2;
if(cnode && cnode->next){
tempnode1 = cnode;
cnode = cnode->next;
head = cnode;
tempnode2 = cnode->next;
cnode->next = tempnode1;
cnode->next->next = tempnode2;
cnode = cnode->next;
}
while(cnode->next && cnode->next->next){
tempnode1 = cnode->next;
cnode->next = cnode->next->next;
tempnode2 = cnode->next->next;
cnode->next->next = tempnode1;
cnode->next->next->next = tempnode2;
cnode = cnode->next->next;
}
return head;
}
void swap_pair(lnode **head)
{
if(!*head)
return;
lnode* prev = 0;
lnode* fst = *head;
lnode* scd = fst->nxt;
if(!scd)
return;
*head = scd;
while(1)
{
fst->nxt = scd->nxt;
scd->nxt = fst;
if(prev)
prev->nxt = scd;
if(!fst->nxt || !fst->nxt->nxt)
break;
prev = fst;
fst = fst->nxt;
scd = fst->nxt;
}
}
void reverseAlternateNodes(struct node **head)
- HelpThyDeveloper September 06, 2008{
struct node *p1 = *head;
struct node *prev = NULL, *temp;
if(p1 == NULL || p1->link == NULL)
return;
else
{
*head = p1->link;
while(p1 != NULL && p1->link != NULL)
{
if(prev != NULL)
prev->link = p1->link;
temp = p1->link->link;
p1->link->link = p1;
p1->link = temp;
prev = p1;
p1 = p1->link;
}
}
}