想成为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; }
相关文章推荐
- 想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(五)
- 想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(六)
- 想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(一)
- 想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(三)
- 想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(四)
- 想成为Google工程师?先回答这15个面试问题
- 想成为Google工程师?先回答这15个面试问题
- Google资深工程师推荐Python面试必须要看的15个问题
- 15个顶级Java多线程面试题及回答 / Java程序员面试中的多线程问题
- 软件测试工程师面试问题及强人的回答(转贴)
- 谷歌面试中,15个让人疯狂的经典问题,你能回答几个?
- 15个顶级Java多线程面试题及回答 / Java程序员面试中的多线程问题
- Google:15个让人疯狂的面试问题
- 15个顶级Java多线程面试题及回答 / Java程序员面试中的多线程问题
- java高级工程师面试问题集结号(four)
- 成为 Google 认证机器学习工程师,零基础也只需要 2 步!
- 16个经典面试问题的回答思路
- 16个经典面试问题回答思路
- 16个经典面试问题回答思路
- 【转载】google 的21道面试问题