NVIDIA Interview Question
Software Engineer / Developers#include <stdio.h>
#include <stdlib.h>
typedef struct node {
struct node *next;
int data;
} Node;
Node* create_node(int data)
{
Node* ptr = (Node*) malloc (sizeof(Node));
ptr->next=NULL;
ptr->data=data;
return ptr;
}
add_to_list(Node** head,Node* new_node)
{
if(*head==NULL)
*head=new_node;
else
{
new_node->next = *head;
*head=new_node;
}
}
print_list(Node* rover)
{
while(rover!=NULL)
{
printf("%d => ",rover->data);
rover = rover->next;
}
printf("NULL\n");
}
Node* reverse_list(Node* head)
{
if(head==NULL)
return;
Node *prev=head,*cur=head->next,*next=head->next->next;
prev->next=NULL;
while(next!=NULL)
{
cur->next = prev;
prev = cur; cur = next; next = next->next;
}
cur->next = prev;
head = cur;
return head;
}
int main()
{
int i=0;
Node *ptr,*head=NULL;
for(i=10;i>0;i--)
{
ptr = create_node(i);
add_to_list(&head,ptr);
}
print_list(head);
head=reverse_list(head);
print_list(head);
getch();
}
void reverse_diffn(dnode **dpnode)
{
dnode *curnode = *dpnode;
unsigned int prevaddr = NULL, curraddr = NULL;
while (curnode != NULL)
{
curraddr = (unsigned int)curnode;
curnode = curnode->down;
*((unsigned int *)curraddr + 1) = (prevaddr);
prevaddr = curraddr;
}
*dpnode = ((dnode *)curraddr);
}
my solution in C:
}
- woohoo February 27, 2011