Microsoft Interview Question for Software Engineer in Tests Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 0 vote

node* reverse(node *ptr)
{
if(ptr->next==NULL){head=ptr; return prt;}
reverse(node->next)->next=ptr;
}

- Samartha July 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 vote

You can find it at :
<a href="http://www.cracktheinterview.net/viewtopic.php?f=15&t=48"> Here</a>

- ItsMe June 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well the solution to this problem would be to take three pointers pointing to first, second and third... make the second pointer next to point to first and first would be second, second would be third and third would be third next.. do check for end condition and case where the list is null, one element and two elements ..

- rkanth May 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

forgot to mention though implicit you need to iterate thro till the third is NULL

- Anonymous May 22, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* reverse(node* head)
{
node* next;
node* prev=NULL;
node* curr=head;

if(!head) return head;
while(curr->next != NULL)
{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
// This is required else the last node pointer is left dangling.
curr->next=prev;

return curr;

}

- newcup June 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Change "while(curr->next != NULL)" to "while(curr!= NULL)" then NO need do last part of "curr->next=prev"

- Anonymous August 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I always see verbose solutions with three pointers... where does it come from?

N* reverse(N* head)
{
N* prev = NULL;

while(head)
{
N* next = head->next; // 1st store it
head->next = prev; // then change it
prev = head;
head = next;

}
return prev;
}

- 8x July 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

/*Write code to reverse a singly linked list*/
#include<stdlib.h>
#include<stdio.h>

struct NODE{
int data;
struct NODE* next;
};
typedef struct NODE* node;

node getNode(){
node x;
x=(node)malloc(sizeof(struct NODE));
return x;
}
node createList(node head,int data){
node temp,cur;
if(!head){
head=getNode();
head->data=0;
head->next=NULL;
}
cur=head;
while(cur->next){
cur=cur->next;
}
temp=getNode();
temp->data=data;
temp->next=NULL;
cur->next=temp;
return head;
}
node reverseList(node head){
node cur,next,prev;
prev=NULL;
cur=head;
next=cur->next;
while(cur->next){
cur->next=prev;
prev=cur;
cur=next;
next=next->next;
}
head=prev;
return head;
}
void display(node head){
node cur;
if(!head){
printf("\nEmpty list");
return;
}
cur=head;
printf("\n");
while(cur->next){
printf("%d ",cur->data);
cur=cur->next;
}
printf("\n\n");

}
int main(){
int i;
node head;
head=NULL;
for(i=1;i<10;i++)
head=createList(head,i);
printf("\nThe original list is :\n");
display(head);
printf("\nThe reversed list is :\n");
head=reverseList(head);
display(head);
return(0);
}
/*
The original list is :

0 1 2 3 4 5 6 7 8


The reversed list is :

8 7 6 5 4 3 2 1

Press any key to continue . . .*/

- Java Coder August 17, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the recursive version for the same.
node* reverse(node* h)
{
....if(!h || !h->next)
........return h;
.....node* temp = reverse(h->next);
.....h->next->next = h;
.....h->next = null;
.....return temp;
}

- Manu September 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rev(struct node **p)
{
sn *a,*b,*c;
a=*p;
b=NULL;
while(a!=NULL)
{
c=b;
b=a;
a=a->next;
b->next=c;
}
*p=b;
}

- rajnesh October 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ListNode* ReverseRecursively(ListNode* listHead) {
...static ListNode* tail = NULL;

...if (tail == NULL) {
......tail = listHead->Next;
......listHead->Next = NULL;
...}

...ListNode* tmp = tail->Next;
...tail->Next = listHead;
...listHead = tail;
...tail = tmp;

...if (tail == NULL) {
......return listHead;
...}
...else {
......return ReverseRecursively(listHead);
...}
}

- Mehmet November 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rev(struct node **head)
{
struct node *first=*head;
struct node *rest=first->next;
if(rest==NULL)
return ;
rev(&rest);
first->next->next=first;
first->next=NULL;
*head=rest;
}

- rajnesh November 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

similar to rajnesh solution

struct Node * reverseList(struct Node * head) {
struct Node * curNode = head ;
struct Node * nextNode = NULL ;
if ((curNode==NULL)||(curNode->next==NULL)){
return head ;
}
nextNode = reverseList(curNode->next) ;
curNode->next->next = curNode ;
curNode->next = NULL ;
return nextNode;
}

- junma August 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

similar to rajnesh solution

struct Node * reverseList(struct Node * head) {
struct Node * curNode = head ;
struct Node * nextNode = NULL ;
if ((curNode==NULL)||(curNode->next==NULL)){
return head ;
}
nextNode = reverseList(curNode->next) ;
curNode->next->next = curNode ;
curNode->next = NULL ;
return nextNode;
}

- junma August 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 0 vote

I liked this link better

http://www.openasthra.com/c-tidbits/reversing-single-linked-list-recursively/

- AD September 16, 2008 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More