Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Iterative version::
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linkedlist{
int val;
struct linkedlist *next;
}ll;
ll *start;
ll *add_node_last(ll *start,int value){
ll *temp=NULL,*temp1=NULL;
if(start!=NULL){
temp=malloc(sizeof(ll));
temp1=start;
temp->val=value;
temp->next=NULL;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
return start;
}
else{
start=malloc(sizeof(ll));
start->val=value;
start->next=NULL;
return start;
}
}
void print_ll(ll *start){
ll *temp=NULL;
temp=start;
while(temp!=NULL){
printf("%d->",temp->val);
temp=temp->next;
}
printf("\n");
}
ll *reverse_k_element(ll *start,int k){
ll *t=NULL,*t1=NULL,*t2=NULL,*start1=NULL,*p=NULL;
int count=0;
int i=1;
t=start;
t2=start;
while(1){
while(i<k&&t->next!=NULL){
t1=t->next;
t->next=t1->next;
t1->next=t2;
t2=t1;
i++;
if(p!=NULL){
p->next=t1;
}
}
if(i==k){
p=t;
count++;
if(count==1){
start1=t2;
}
t=t->next;
t2=t;
i=1;
}
else if(count>=1)
return start1;
else
return t1;
}
return t;
}
main(){
start=NULL;
ll *temp=NULL,*temp1=NULL;
int n=8,i=1,k=3;
//start=malloc(sizeof(ll));
while(i<=n){
start=add_node_last(start,i);
i++;
}
temp=reverse_k_element(start,k);
print_ll(temp);
}
Recursive version
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linkedlist{
int val;
struct linkedlist *next;
}ll;
ll *start;
ll *add_node_last(ll *start,int value){
ll *temp=NULL,*temp1=NULL;
if(start!=NULL){
temp=malloc(sizeof(ll));
temp1=start;
temp->val=value;
temp->next=NULL;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
return start;
}
else{
start=malloc(sizeof(ll));
start->val=value;
start->next=NULL;
return start;
}
}
void print_ll(ll *start){
ll *temp=NULL;
temp=start;
while(temp!=NULL){
printf("%d->",temp->val);
temp=temp->next;
}
printf("\n");
}
ll *reverse_k_element(ll *start,int k){
ll *t=NULL,*t1=NULL,*t2=NULL,*start1=NULL,*p=NULL;
int count=0;
int i=1;
t=start;
t2=start;
while(1){
while(i<k&&t->next!=NULL){
t1=t->next;
t->next=t1->next;
t1->next=t2;
t2=t1;
i++;
if(p!=NULL){
p->next=t1;
}
}
if(i==k){
p=t;
count++;
if(count==1){
start1=t2;
}
t=t->next;
t2=t;
i=1;
}
else if(count>=1)
return start1;
else
return t1;
}
return t;
}
main(){
start=NULL;
ll *temp=NULL,*temp1=NULL;
int n=8,i=1,k=3;
//start=malloc(sizeof(ll));
while(i<=n){
start=add_node_last(start,i);
i++;
}
temp=reverse_k_element(start,k);
print_ll(temp);
}
sorry here is the recursive version:::
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linkedlist{
int val;
struct linkedlist *next;
}ll;
ll *start;
ll *add_node_last(ll *start,int value){
ll *temp=NULL,*temp1=NULL;
if(start!=NULL){
temp=malloc(sizeof(ll));
temp1=start;
temp->val=value;
temp->next=NULL;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
return start;
}
else{
start=malloc(sizeof(ll));
start->val=value;
start->next=NULL;
return start;
}
}
void print_ll(ll *start){
ll *temp=NULL;
temp=start;
while(temp!=NULL){
printf("%d->",temp->val);
temp=temp->next;
}
printf("\n");
}
ll *reverse_k_element(ll *start,int k){
ll *t=NULL,*t1=NULL,*t2=NULL,*start1=NULL,*t3=NULL;
int i=1;
if(start==NULL)
return NULL;
else if(start->next==NULL)
return start;
else{
t=start;
t2=t;
while(i<k&&t->next!=NULL){
t1=t->next;
t->next=t1->next;
t1->next=t2;
t2=t1;
i++;
}
if(t->next!=NULL){
t3=reverse_k_element(t->next,k);
t->next=t3;
return t2;
}
else
return t2;
}
}
main(){
start=NULL;
ll *temp=NULL,*temp1=NULL;
int n=8,i=1,k=3;
//start=malloc(sizeof(ll));
while(i<=n){
start=add_node_last(start,i);
i++;
}
temp=reverse_k_element(start,k);
print_ll(temp);
}
{
void reverse(int x){// x is the position that is kth element
revhead=head;
link current = revhead;
link head11 = null;
for(int i=0;i<x && current!=null;i++)
{
link save = current;
current = current.nextlink;
save.nextlink = head11;
head11 = save;
}
while(head11!=null){
System.out.print(" Data "+head11.data);//printing the new reverse list
head11 = head11.nextlink;
}
System.out.println();
}
}
Please comment on below code if you can suggest to improve it more...I am new to C++
void lList::reverseKelement(int k)
{
list *temp = l;
list *temp1 = NULL;
list *temp2 =NULL;
while(k!=0)
{
temp2 = temp->next;
temp->next = temp1;
temp1 = temp;
temp = temp2;
k--;
}
l->next = temp;
l = temp1;
}
This will only work if we need to reverse k elements and there are K elements in the list but not for multiples of K.
Eg. If list has 5 elements and K is 5, this will work, but if we have 10 elements and we need to reverse every 5 elements, this wont work, trying coding it, you will understand
It's simple, it starts pointing to the end of the list, try adding one more value to this list with an insert statement, and then you won't get this error.
- Ashu May 23, 2012