您的位置:首页 > 职场人生

想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(二)

2012-02-07 22:34 579 查看
第二题,合并两个有序链表。

2.合并两个排序链表
挑战: 这类问题是Google面试问题的一个共同趋势:找出解决问题的有效办法。

合并两条链表是一般会在链表之间发生“冲突”(因为它们各自有特定的次序,而你的合并会把次序搞乱)

你必须找出一种算法快速消除那些冲突。
不是很理解它里面所谓的”冲突“,是否包含着其他玄机,^_^,先用简单的写法写一个吧,不然,合并两个升序的链表。

//program used to merge two  sort list
#include<iostream>
#include<fstream>

using namespace std;

ofstream fout("output.txt");

typedef struct Node
{
int data;
Node * next;
} *List;

List list1 = NULL;
List list2 = NULL;

//generate the two list
void generateList()
{
int data1[10] = {2, 4, 8, 10, 12, 14, 18, 19, 30, 50};
int data2[8] = { 3, 5, 9, 10, 14, 28, 34, 59 };

for (int i=9; i >= 0; i --)
{
Node * temp = new Node();
temp->data = data1[i];
temp->next = list1;
list1 = temp;
}//end for data1

for (int i=7; i>=0; i--)
{
Node * temp = new Node();
temp->data = data2[i];
temp->next = list2;
list2 = temp;
}//end for data2
}

//merge the two sort list
List mergesort(List list1, List list2)
{
List result, cur;

//initial the head of the list
if (list1->data > list2->data)
{
result = list2;
cur = list2;
list2 = list2->next;
}
else
{
result = list1;
cur = list1;
list1 = list1->next;
}//end if ... else

while(list1 != NULL && list2 != NULL)
{
if (list1->data > list2->data)
{
cur->next = list2;
cur = list2;
list2 = list2->next;
}
else
{
cur->next = list1;
cur = list1;
list1 = list1->next;
}
}//END WHILE
return result;
}

//print the list
void print(List list)
{
while(list)
{
cout << list->data << " ";
list= list->next;
}
cout << endl;
}

int main()
{
//generate the list
generateList();

//print the list
print(list1);
print(list2);

//merge the sort list
List m = mergesort(list1, list2);

//print the result
print(m);
return 0;
}
这个还是比较简单的,比较难写的是单链表快排,这是以前写的一个单链表快排,一并附上作为一个参考吧。

#include <iostream>
#include <stdlib.h>

using namespace std;

struct Node
{
int data;
Node* next;
};

Node * generate()
{
Node * head = (Node *)malloc(sizeof(Node));
head->data = rand()%100 + 1;
head->next = NULL;

for (int i=0; i < 10; i ++)
{
Node * temp = (Node*)malloc(sizeof(Node));
temp->data = rand()%100 + 1;
temp->next = head->next;
head->next = temp;
}
return head;
}

void printList(Node * head)
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}

Node * quicksortpass(Node **start, Node* end)
{
Node * provit = *start;
Node * left = provit;
Node * right = provit;

(*start) = (*start)->next;
while((*start) != end){
Node * temp = (*start);
(*start) = (*start)->next;
if(temp->data < provit->data)
{
temp->next = left;
left = temp;
}
else
{
right->next = temp;
right = temp;
}
}
right->next = end;
*start = left;
return provit;
}

void quicksortlist(Node ** start, Node * end )
{
if(*start != end)
{
Node * provit = quicksortpass(start, end);
quicksortlist(start, provit);
quicksortlist(&(provit->next), end);
}
}

int main()
{
Node * list = generate();
printList(list);

quicksortlist(&list, NULL);
printList(list);

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: