Amazon Interview Question
Software Engineer / DevelopersCountry: United States
public static ListNode mergeLists(ListNode l1, ListNode l2) {
ListNode l3 = null;
ListNode merged = null;
if(l1 == null)
return l2;
if(l2 == null)
return l1;
l3 = l1;
l3.next = l2;
merged = l3;
l1 = l1.next;
l2 = l2.next;
l3 = l3.next;
while(l1 != null || l2 != null) {
if(l1 != null) {
l3.next = l1;
l3 = l3.next;
l1 = l1.next;
}
if(l2 != null) {
l3.next = l2;
l3 = l3.next;
l2 = l2.next;
}
}
return merged;
}
where ListNode is something like:
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
int mergrList(Node first,Node second){
Note temp1,temp2;
temp1 = first.next;
temp2 = second.next;
while(first != null && second != null){
first.next = second;
second.next = temp1;
first = temp1;
second = temp2;
temp1 = first.next;
temp2 = second.next;
}
// If any one is empty copy the remaining as such in the link.
}
Below is a perfectly working code,, it will be in place merging as no additional memory is required (other than storing pointer)
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
struct _node{
int n;
struct _node *pNext;
};
typedef struct _node node;
enum _listType{
even =1,
odd
};
typedef enum _listType listType;
node* intermixList(node *pListOne, node *pListTwo);
node* createList (int iNodes, int evenOdd);
void displayList(node *pCurr);
void mixList(node *pListOne, node *pListTwo);
main()
{
node *pListOne, *pListTwo, *pStartList, *pCurr, *pNode;
int i;
listType lt;
pListOne = pListTwo = NULL;
lt = even;
pListOne = createList(8, 2);
pListTwo = createList(5, 1);
displayList(pListOne);
displayList(pListTwo);
mixList(pListOne, pListTwo);
displayList(pListOne);
}
node* createList (int iNodes, int evenOdd)
{
int i, j, k ;
node *pNode, *pStart, *pCurr;
pNode = (node *) malloc( 1 * sizeof(node));
pNode->n = ((evenOdd ==1) ? 2 : 1);
pNode->pNext = NULL;
pStart = pNode;
k = (evenOdd == 1) ? 4:3;
for (i = k, j = 1; j<=iNodes - 1; i=i+2, j++)
{
pCurr = (node *) malloc( 1 * sizeof(node));
pCurr->n = i;
pNode->pNext = pCurr;
pNode = pCurr;
}
pNode->pNext = NULL;
return pStart;
}
void displayList(node *pCurr)
{
printf("Started printing the List\n");
while(pCurr != NULL)
{
printf("\tValue in node is %d\n", pCurr->n);
pCurr= pCurr->pNext;
}
printf("Ended printing the List\n\n");
return;
}
void mixList(node *pListOne, node *pListTwo)
{
node *pTemp1, *pTemp2, *pFirstList, *pSecondList;
pFirstList = pListOne;
pSecondList = pListTwo;
if(pFirstList->pNext == NULL)
{
pFirstList->pNext = pSecondList;
return;
}
else
{
while((pSecondList->pNext != NULL) && (pFirstList->pNext != NULL))
{
pTemp1 = pFirstList->pNext;
pFirstList->pNext = pSecondList;
pTemp2 = pSecondList->pNext;
pSecondList->pNext = pTemp1;
pFirstList = pTemp1;
pSecondList = pTemp2;
}
if (pFirstList->pNext == NULL)
{
pFirstList->pNext = pSecondList;
}
else if (pSecondList->pNext == NULL)
{
pTemp1 = pFirstList->pNext;
pFirstList->pNext = pSecondList;
pSecondList->pNext = pTemp1;
}
}
return;
}
@hussain this code also changes the input lists, other way to do the same thing without altering i/p lists would be to
1. create an empty linked list
2. start moving a pointer on each linked list
3. in each iteration add the data in the new linkedlist
4. if any of the linked list has reached the tail then just append the other one to the new linked list
- Anonymous March 28, 2012