Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Possible solution: Parse the list and push alternate nodes on the stack. Then pop and add at the end.
@Victor: Code in c to support your statement
#include <stdio.h>
struct node_{
int data;
struct node_ *next;
};
typedef struct node_ node;
node *head = NULL;
int add(node **head,int no)
{
node *temp,*head_ref;
head_ref = *head;
temp = malloc(sizeof(struct node_));
temp->data = no;
temp->next = NULL;
if(*head == NULL){
*head = temp;
} else {
while(head_ref->next != NULL)
{
head_ref = head_ref->next;
}
head_ref->next = temp;
}
return 0;
}
void print(node *head)
{
while(head != NULL)
{
printf("node no %d \n",head->data);
head = head->next;
}
}
void add_alt_nodes(node *temp, node **list)
{
if(temp == NULL)
return;
add(list, temp->data);
}
void add_alt_data(int data, node **list)
{
add(list, data);
}
void push(node **list, int data)
{
node *temp = *list;
node *t = malloc(sizeof(*t));
t->data = data;
t->next = NULL;
if(temp == NULL) {
*list = t;
} else {
/* find the top most element */
t->next = *list;
*list = t;
}
}
int pop(node **list)
{
int data;
if(!*list)
return -1;
data = (*list)->data;
(*list) = (*list)->next;
return data;
}
int main()
{
int i = 0;int found;int data=0;
node *temp;
node *s_list=NULL;
node *s_list_1=NULL;
for(i=0;i<20;i++)
add(&head, i);
for(temp=head;temp && temp->next;temp=temp->next->next) {
add_alt_nodes(temp, &s_list);
}
add_alt_nodes(temp, &s_list);
for(temp=head->next;temp && temp->next;temp=temp->next->next) {
push(&s_list_1, temp->data);
}
if(temp)
push(&s_list_1, temp->data);
while(1) {
data = pop(&s_list_1);
if(data == -1)
break;
add_alt_data(data, &s_list);
}
print(s_list);
return 0;
}
Thanks for the question. This was surprisingly tricky to get right...I made mistakes. I think part of the reason was that I underestimated the question :-(
The foll code works for 1, 1-901, 1-901-2, 1-901-2-902.
if (head->next) {
second_head = head->next;
} else {
printf("Only one element in list.\n");
return 1;
}
if (second_head->next == NULL) {
printf("Only two elements in the list.\n");
return 2;
}
tail = head;
second_tail = second_head;
while(tail->next && second_tail && second_tail->next) {
tail->next = tail->next->next;
second_tail->next = second_tail->next->next;
tail = tail->next;
second_tail = second_tail->next;
}
// attach the first and second lists togther.
tail->next = second_head;
print_list(head);
This might do it:
ListNode* reverseList(ListNode* head)
{
if(head == NULL || head->next == NULL) return head;
ListNode *pre = NULL, *nex;
for(; head != NULL; pre = head, head = nex){
nex = head->next;
head->next = pre;
}
return pre;
}
ListNode* reverseAlternateAppendToTail(ListNode* head)
{
if(head == NULL || head->next) return head;
//break into two lists
ListNode *list1 = head, *list2 = head->next, *p1 = list1, *p2 = list2, *p = list2->next;
bool first = true;
for(; p != NULL; p = p->next, first = !first){
if(first){
p1->next = p;
p1 = p1->next;
}
else{
p2->next = p;
p2 = p2->next;
}
}
//reverse list2 and append it to list1's tail
p2->next = NULL;
p1->next = reverseList(list2);
return list1;
}
void ReverseAltNodes(struct Node *&node)
{
if(!node || !node->next)
return;
struct Node *last=node;
while(last->next)
last=last->next;
struct Node *t=node;
while(t && t!=last && t->next!=last)
{
struct Node *next=t->next;
if(!next){
break;
}
t->next=next->next;
struct Node *s=last->next;
last->next=next;
next->next=s;
t=t->next;
}
}
struct Node {
Node(char d) : data(d), next(0) {}
char data;
Node* next;
};
inline void ADD(Node*& t, Node* n)
{
n->next = 0; t->next = n; t = n;
}
int main(int argc, char** argv)
{
Node* head = new Node('a');
Node* tail = head;
ADD(tail, new Node('x'));
ADD(tail, new Node('b'));
ADD(tail, new Node('y'));
ADD(tail, new Node('c'));
ADD(tail, new Node('z'));
for (Node* p = head; p->next != tail; p = p->next) {
Node* t = p->next;
p->next = t->next;
t->next = tail->next;
tail->next = t;
}
for (Node* n = head; n; n = n->next) cout << n->data; cout << endl;
cout << endl;
}
private static Node reverseAlternateElement(Node start) {
if(start==null || start.next==null)
return start;
Node startCopy=start;
Node second= start.next;
Node secondCopy=second;
while(second!=null && second.next!=null){
start.next=second.next;
start=start.next;
second.next=start.next;
second=second.next;
}
start.next=secondCopy;
return startCopy;
}
Didnt include corner cases where the list is empty or has only one element....
- Jayanth February 07, 2014