Amazon Interview Question
Senior Software Development EngineersCountry: India
Interview Type: In-Person
Recursive solution in Java...
public static Node swapPairsInList(Node head){
if(head == null || head.next == null) return head;
Node next = null;
Node tail = null;
if(head.next != null){
next = head.next;
tail = next.next;
next.next = head;
head.next = tail;
}
if(tail != null && tail.next != null){
head.next = swapPairsInList(tail);
}
return next;
}
Recursive solution in Java
public static Node swapPairsInList(Node head){
if(head == null || head.next == null) return head;
Node next = null;
Node tail = null;
if(head.next != null){
next = head.next;
tail = next.next;
next.next = head;
head.next = tail;
}
if(tail != null && tail.next != null){
head.next = swapPairsInList(tail);
}
return next;
}
typedef struct list_item{
int dummy_data;
list_item *next;
}list_item;
list_item *
reverse_list(list_item *header)
{
list_item *first = header;
list_item *second = NULL;
list_item *previous = NULL;
list_item *new_header = header;
// we determine the new header first
if(first != NULL)
{
if (first->next != NULL)
{
// swap first and second item
second = first->next; // current second move to front
first->next = second->next; // current first should link to the third item
second->next = first; // second then link to current first item
new_header = second; // set the new header
previous = first; // remember new second position
}
first = first->next; // move the first pointer to third item or NULL
}
// now we deal with the rest
while(first != NULL)
{
if (first->next != NULL)
{
previous->next = first->next; // fix the previous item next pointer
second = first->next; // re-assign second
first->next = second->next; //
second->next = first;
previous = first;
}
first = first->next;
}
return new_header;
}
package in.blogspot.randomcompiler;
class Node {
int data;
Node next;
}
public class ReversePairs {
public static void main(String[] args) {
Node node = new Node();
node.data = 1;
Node head = node;
for(int i=2; i<=101; i++) {
Node temp = new Node();
temp.data = i;
node.next = temp;
node=temp;
}
printNodes(head);
head = reversePairs(head);
printNodes(head);
}
private static Node reversePairs(Node head) {
if(head == null || head.next == null) {
return head;
}
Node headNextNext = head.next.next;
head.next.next = head;
Node newHeadNext = reversePairs(headNextNext);
Node newHead = head.next;
head.next = newHeadNext;
return newHead;
}
private static void printNodes(Node head) {
while(head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.print("null");
System.out.println();
}
}
Output:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14 -> 15 -> 16 -> 17 -> 18 -> 19 -> 20 -> 21 -> 22 -> 23 -> 24 -> 25 -> 26 -> 27 -> 28 -> 29 -> 30 -> 31 -> 32 -> 33 -> 34 -> 35 -> 36 -> 37 -> 38 -> 39 -> 40 -> 41 -> 42 -> 43 -> 44 -> 45 -> 46 -> 47 -> 48 -> 49 -> 50 -> 51 -> 52 -> 53 -> 54 -> 55 -> 56 -> 57 -> 58 -> 59 -> 60 -> 61 -> 62 -> 63 -> 64 -> 65 -> 66 -> 67 -> 68 -> 69 -> 70 -> 71 -> 72 -> 73 -> 74 -> 75 -> 76 -> 77 -> 78 -> 79 -> 80 -> 81 -> 82 -> 83 -> 84 -> 85 -> 86 -> 87 -> 88 -> 89 -> 90 -> 91 -> 92 -> 93 -> 94 -> 95 -> 96 -> 97 -> 98 -> 99 -> 100 -> 101 -> null
2 -> 1 -> 4 -> 3 -> 6 -> 5 -> 8 -> 7 -> 10 -> 9 -> 12 -> 11 -> 14 -> 13 -> 16 -> 15 -> 18 -> 17 -> 20 -> 19 -> 22 -> 21 -> 24 -> 23 -> 26 -> 25 -> 28 -> 27 -> 30 -> 29 -> 32 -> 31 -> 34 -> 33 -> 36 -> 35 -> 38 -> 37 -> 40 -> 39 -> 42 -> 41 -> 44 -> 43 -> 46 -> 45 -> 48 -> 47 -> 50 -> 49 -> 52 -> 51 -> 54 -> 53 -> 56 -> 55 -> 58 -> 57 -> 60 -> 59 -> 62 -> 61 -> 64 -> 63 -> 66 -> 65 -> 68 -> 67 -> 70 -> 69 -> 72 -> 71 -> 74 -> 73 -> 76 -> 75 -> 78 -> 77 -> 80 -> 79 -> 82 -> 81 -> 84 -> 83 -> 86 -> 85 -> 88 -> 87 -> 90 -> 89 -> 92 -> 91 -> 94 -> 93 -> 96 -> 95 -> 98 -> 97 -> 100 -> 99 -> 101 -> null
public class ReversePairsLL {
public static Node reversePairs(Node f) {
if(f == null) return null;
Node prev = null;
Node cur = f;
Node newHead = f.next;
while(cur !=null && cur.next !=null) {
Node temp = cur.next;
cur.next = temp.next;
if(prev == null) {
temp.next = cur;
prev = cur;
} else {
temp.next = cur;
prev.next = temp;
prev =cur;
}
cur = cur.next;
}
return newHead;
}
public static void main(String[] args) {
Node first = new Node(1);
Node cur = first;
for(int i=2;i <6;i++) {
Node n = new Node(i);
cur.next = n;
cur = cur.next;
}
cur=first;
while(cur != null) {
System.out.print(" " + cur.info);
cur = cur.next;
}
System.out.println();
Node nhead = reversePairs(first);
cur = nhead;
while(cur != null) {
System.out.print(" " + cur.info);
cur = cur.next;
}
System.out.println();
}
}
class Node {
public int info;
public Node next;
Node(int data) {
info = data;
next = null;
}
}
public static Node Reverse(Node node)
{
var head = node;
if (node == null)
{
return head;
}
var i1 = node;
if (i1.Next == null)
{
return head;
}
var i2 = i1.Next;
head = i2;
Node previ1 = null;
while (i2.Next != null)
{
i1.Next = i2.Next;
i2.Next = i1;
if (previ1 != null)
{
previ1.Next = i2;
}
previ1 = i1;
i1 = i1.Next;
if (i1.Next == null)
{
return head;
}
i2 = i1.Next;
}
return head;
}
}
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
typedef struct _list
{
int data;
_list *next;
} list;
int arr[11] = {78, 40, 25, 40, 17, 65, 3, 47, 54, 98, 23};
void preplist(list **head, int arr[], int size){
list **cur = head;
int i = size;
while (i){
list *temp = (list*)calloc(sizeof(list), 1);
assert(temp != NULL);
temp->data = arr[--i];
temp->next = *cur;
*cur = temp;
}
}
void displist(list* head){
printf("\nProduced List :: ");
while (head){
printf("%d->", head->data);
head = head->next;
}
printf("NULL");
}
void smartReverse(list **head){
if (*head == NULL || (*head)->next == NULL)
return;
list *first = *head;
list *second = first->next;
smartReverse(&(second->next));
first->next = second->next;
second->next = first;
*head = second;
}
int main(){
list *head = NULL;
preplist(&head, arr, SIZE);
displist(head);
smartReverse(&head);
displist(head);
getchar();
return 0;
}
OutPut
Produced List :: 78->40->25->40->17->65->3->47->54->98->23->NULL
Produced List :: 40->78->40->25->65->17->47->3->98->54->23->NULL
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
typedef struct _list
{
int data;
_list *next;
} list;
int arr[11] = {78, 40, 25, 40, 17, 65, 3, 47, 54, 98, 23};
void preplist(list **head, int arr[], int size){
list **cur = head;
int i = size;
while (i){
list *temp = (list*)calloc(sizeof(list), 1);
assert(temp != NULL);
temp->data = arr[--i];
temp->next = *cur;
*cur = temp;
}
}
void displist(list* head){
printf("\nProduced List :: ");
while (head){
printf("%d->", head->data);
head = head->next;
}
printf("NULL");
}
void smartReverse(list **head){
if (*head == NULL || (*head)->next == NULL)
return;
list *first = *head;
list *second = first->next;
smartReverse(&(second->next));
first->next = second->next;
second->next = first;
*head = second;
}
int main(){
list *head = NULL;
preplist(&head, arr, SIZE);
displist(head);
smartReverse(&head);
displist(head);
getchar();
return 0;
}
OutPut
Produced List :: 78->40->25->40->17->65->3->47->54->98->23->NULL
Produced List :: 40->78->40->25->65->17->47->3->98->54->23->NULL
- Anonymous April 27, 2015